学习Python网络数据采集时,讲到标签的查找
from bs4 import BeautifulSoup from urllib.request import urlopen import re html = urlopen('http://www.pythonscraping.com/pages/page3.html') bsObj = BeautifulSoup(html,"html.parser") children = bsObj.findAll('table',{'id':'giftList'}).tr.next_siblings for child in children: print(child)
因为一直在想findAll()和find()的区别,于是就改了一下代码,发现报错。
Traceback (most recent call last):
File "G:/python/reptile/test.py", line 6, in <module>
children = bsObj.findAll('table',{'id':'giftList'}).tr.next_siblings
File "G:\Users\DELL\python\lib\site-packages\bs4\element.py", line 1807, in __getattr__
"ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute 'tr'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?
然后查了一下百度发现有人和我一样的。然后了解到:
对象的方法find_all()返回的是一个结果集列表,而结果集列表是一组数据,其是没有属性的,只有单个数据对象才有属性可言。
应该使用find().children,而soup.find()返回的结果一个字符串对象,其可以存在属性的调用。
原网址:https://www.cnblogs.com/my1e3/p/6680476.html
2058





