使用 urllib 在 python 中删除换行符 [英] Remove newline in python with urllib
问题描述
我使用的是 Python 3.x.在使用 urllib.request
下载网页时,我收到了很多 \n
介于两者之间.我正在尝试使用论坛其他线程中给出的方法将其删除,但我无法这样做.我使用过 strip()
函数和 replace()
函数......但没有运气!我在 Eclipse 上运行此代码.这是我的代码:
I am using Python 3.x. While using urllib.request
to download the webpage, i am getting a lot of \n
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 strip()
function and the replace()
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({})
try:
open_url = opener.open(a)
page = str(open_url.read())
return page
except:
return""
raw_html = download_page("http://www.zseries.in")
print("Raw HTML = " + raw_html)
#Remove line breaks
raw_html2 = raw_html.replace('\n', '')
print("Raw HTML2 = " + raw_html2)
我无法找出在 raw_html
变量中获得大量 \n
的原因.
I am not able to spot out the reason of getting a lot of \n
in the raw_html
variable.
推荐答案
好像它们是文字 \n
字符,所以我建议你这样做.
Seems like they are literal \n
characters , so i suggest you to do like this.
raw_html2 = raw_html.replace('\\n', '')
这篇关于使用 urllib 在 python 中删除换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!