hnvasa hnvasa - 1 year ago 130
Python Question

Remove newline in python with urllib

I am using Python 3.x. While using

to download the webpage, i am getting a lot of
in between. I am trying to remove it using the methods given in the other threads of the forum, but i am not able to do so. I have used
function and the
function...but no luck! I am running this code on eclipse. Here is my code:

import urllib.request

#Downloading entire Web Document
def download_page(a):
opener = urllib.request.FancyURLopener({})
open_url =
page = str(
return page
raw_html = download_page("")
print("Raw HTML = " + raw_html)

#Remove line breaks
raw_html2 = raw_html.replace('\n', '')
print("Raw HTML2 = " + raw_html2)

I am not able to spot out the reason of getting a lot of
in the

Answer Source

Seems like they are literal \n characters , so i suggest you to do like this.

raw_html2 = raw_html.replace('\\n', '')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download