【发布时间】:2014-02-28 10:03:11
【问题描述】:
编辑:我不敢相信 BeautifullSoup 实际上无法正确解析 HTML。实际上我可能做错了什么,但如果我不这样做,这是一个非常业余的模块。
我正在尝试从网络上获取文本,但我无法这样做,因为我总是在大多数句子中得到一些奇怪的字符。我从来没有得到一个包含诸如“不正确”之类的词的句子。
useragent = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11'}
request = urllib2.Request('SomeURL',None,useragent)
myreq = urllib2.urlopen(request, timeout = 5)
html = myreq.read()
#get paragraphs
soup = BeautifulSoup(html)
textList = soup.find_all('p')
mytext = ""
for par in textList:
if len(str(par))<2000:
print par
mytext +=" " + str(par)
print "the text is ", mytext
结果包含一些奇怪的字符:
The plural of “comedo� is comedomes�.</p>
Surprisingly, the visible black head isn’t caused by dirt
显然我想得到不是而不是不是。我该怎么办?
【问题讨论】:
-
这不是重复的。我首先需要提取所有段落。我认为解码会删除所有
标签。
-
我现在需要告诉 beautifullsoup 来毁掉我的 html。我不敢相信这样一个有信誉的 python 模块不能正确解析 html。
标签: python python-2.7 text web-scraping beautifulsoup