Detecting BeautifulSoup Freeze

狂风中的少年 提交于 2020-01-05 12:08:13

问题


Parsing the following site using beautifulsoup with html5lib parser hangs but the same works well with html.parser

Site: 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')
> [.....]

Is there something wrong with what i am doing or is there a way to detect if soup will fail before making the call to soup('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 reference: http://paste.ubuntu.com/7125462/

Thanks

来源:https://stackoverflow.com/questions/22536072/detecting-beautifulsoup-freeze

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!