python从入门到放弃

学习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

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值