检测BeautifulSoup冻结(Detecting BeautifulSoup Freeze)

2019-10-19 15:41发布

解析使用beautifulsoup与html5lib解析器挂起以下网站,但同样的作品很好地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

引擎收录参考: http://paste.ubuntu.com/7125462/

谢谢

文章来源: Detecting BeautifulSoup Freeze