检测BeautifulSoup Freeze

时间:2014-03-20 14:30:30

标签: python parsing html-parsing beautifulsoup

使用带有html5lib解析器的beautifulsoup解析以下站点会挂起,但同样适用于html.parser

网站:http://www.webmasterworld.com/google/3584866.htm

#contents is fetched via urllib2
soup = BeautifulSoup(contents, 'html5lib')

soup('a') hangs

but the following works just fine

soup = BeautifulSoup(contents1, 'html.parser')

soup('a')
> [.....]

我正在做的事情有什么问题,或者有没有办法在调用汤之前检测汤是否会失败('a')

from bs4 import BeautifulSoup
import urllib2

h = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko)     Chrome/23.0.1271.64 Safari/537.11'}


r = urllib2.Request('http://www.webmasterworld.com/google/3584866.htm', headers=h)

u = urllib2.urlopen(r)

contents = u.read()

### Type1
soup = BeautifulSoup(contents, 'html5lib')
parsed_content = soup('a')
# Hangs here...


### Type2
soup = BeautifulSoup(contents, 'html.parser')
parsed_content = soup('a')
print parsed_content 
# Works

Pastebin参考: http://paste.ubuntu.com/7125462/

由于

0 个答案:

没有答案