美丽的汤和Python的属性错误

时间:2015-04-11 21:33:59

标签: python python-2.7 web-scraping beautifulsoup

我有一段工作代码,然后我今天运行它并且它已经坏了。我已经拿出了给我提问的相关部分。

from bs4 import BeautifulSoup
import requests

webpage = requests.get('http://www.bbcgoodfood.com/search/recipes?query=')

soup = BeautifulSoup(webpage.content) 
links = soup.find("div",{"class":"main row grid-padding"}).find_all("h2",{"class":"node-title"})

for link in links:
    print(link.a["href"]) 

这给了我一个错误“属性错误:'NoneType'对象没有属性'find_all'”

这个错误究竟告诉了我什么?

find_all()是漂亮汤文档中的有效命令。 浏览网页的源代码,我的所需对象的路径似乎有意义。

我认为网站必定会有所改变,因为我看不到我的代码如何停止工作。但我不明白错误信息......

感谢您提供任何帮助!

2 个答案:

答案 0 :(得分:2)

您尝试解析的网站没有"喜欢"您的用户代理并返回403错误,然后解析器失败,因为它找不到div。尝试将user-agent更改为其中一个浏览器的用户代理:

webpage = requests.get('http://www.bbcgoodfood.com/search/recipes?query=', headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'})

答案 1 :(得分:1)

这是因为当您尝试访问该页面时,它会为您提供permission denied,因此soup.find()不返回NoneNone没有find_all()的属性1}},这会给你一个AttributeError

from bs4 import BeautifulSoup
import requests

webpage = requests.get('http://www.bbcgoodfood.com/search/recipes?query=')


print webpage.content
<HTML><HEAD>
<TITLE>Access Denied</TITLE>
</HEAD><BODY>
<H1>Access Denied</H1>

You don't have permission to access "http&#58;&#47;&#47;www&#46;bbcgoodfood&#46;com&#47;search&#47;recipes&#63;" on this server.<P>
Reference&#32;&#35;18&#46;4fa9cd17&#46;1428789762&#46;680369dc
</BODY>
</HTML>

如果您通过添加带有@Vader建议的适当用户代理的标头来解决此问题,那么您的代码将正常运行:

...
headers = {'User-agent': 'Mozilla/5.0'}
webpage = requests.get('http://www.bbcgoodfood.com/search/recipes?query=', headers=headers)

soup = BeautifulSoup(webpage.content) 
links = soup.find("div",{"class":"main row grid-padding"}).find_all("h2",{"class":"node-title"})

for link in links:
    print(link.a["href"])

/recipes/4942/lemon-drizzle-cake
/recipes/3092/ultimate-chocolate-cake
/recipes/3228/chilli-con-carne
/recipes/3229/yummy-scrummy-carrot-cake
/recipes/1223/bestever-brownies
/recipes/1167651/chicken-and-chorizo-jambalaya
/recipes/2089/spiced-carrot-and-lentil-soup
/recipes/1521/summerinwinter-chicken
/recipes/1364/spicy-root-and-lentil-casserole
/recipes/4814/mustardstuffed-chicken
/recipes/4622/classic-scones-with-jam-and-clotted-cream
/recipes/333614/red-lentil-chickpea-and-chilli-soup
/recipes/5605/falafel-burgers
/recipes/11695/raspberry-bakewell-cake
/recipes/4686/chicken-biryani