Urllib2的问题/美丽的汤不刮

时间:2014-05-21 16:34:04

标签: python beautifulsoup urllib2

我很喜欢美丽的汤,但我注意到,当我运行这段代码时,很多时候汤.prettify不打印任何东西。我已经检查过以确保页面正确但仍然没有运气。有什么想法吗?

URL = "http://www.whoscored.com/Search/?t="
print "toURL: %s" %(URL)
request = urllib2.Request(URL)
response = urllib2.urlopen(request)
soup = BeautifulSoup(response)
print soup.prettify()

1 个答案:

答案 0 :(得分:0)

您需要更改用户代理才能获得回复 始终使用print response.info()并在这类案例中查看响应的内容长度,以调试错误。

opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
response = opener.open(URL)
soup = BeautifulSoup(response)
print soup.prettify()