Python的BeautifulSoup之find和find_all
来源:互联网 发布:java yield join 编辑:程序博客网 时间:2024/05/18 21:49
今天学习写爬虫,练习网址为http://blog.csdn.net/bo_wen_/article/details/50868339,做一个抓取每日最高最低温度的练习。在过程中遇到这样一个问题,代码所示:
# coding : UTF-8import requestsfrom bs4 import BeautifulSoupres = requests.get('http://www.weather.com.cn/weather/101190401.shtml')res.encoding = 'utf-8'soup = BeautifulSoup(res.text,"html5lib")tt=soup.body.find_all('ul',class_= 't clearfix')tt2 = tt.find_all('li')print(tt2)
运行结果如下:---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-60-2de0ac4bc00d> in <module>()
14
15 tt=soup.body.find_all('ul',class_= 't clearfix')
---> 16 tt2 = tt.find_all('li')
17 print(tt2)
D:\folder\envs\hh\lib\site-packages\bs4\element.py in __getattr__(self, key)
1805 def __getattr__(self, key):
1806 raise AttributeError(
-> 1807 "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
1808 )
AttributeError: ResultSet object has no attribute 'find_all'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?
-------------------------------------------------------------------------------------------------------------------
纠结了很久,后来看了一些资料发现问题出在对find和find_all这两个函数的理解不够。官方指南原文如下:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/
find( name , attrs , recursive , text , **kwargs )
find_all() 方法将返回文档中符合条件的所有tag,尽管有时候我们只想得到一个结果.比如文档中只有一个<body>标签,那么使用 find_all() 方法来查找<body>标签就不太合适, 使用 find_all 方法并设置 limit=1 参数不如直接使用 find() 方法.下面两行代码是等价的:
唯一的区别是 find_all() 方法的返回结果是值包含一个元素的列表,而 find() 方法直接返回结果.
find_all() 方法没有找到目标是返回空列表, find() 方法找不到目标时,返回 None .
tt=soup.body.find_all('ul',class_= 't clearfix')[0]
- Python的BeautifulSoup之find和find_all
- BeautifulSoup find() 和 find_all()
- python网络爬虫与信息采取之解析网页(二)---BeautifulSoup库的find()和find_all()
- BeautifulSoup中find(),find_all(),select()函数
- python之BeautifulSoup之二 带属性值的抓取(find_all('tag', attrs={'class':'value'})
- Python的BeautifulSoup的find()和findAll()
- Python爬虫:BeautifulSoup的find()和findAll()
- python之find_all
- python爬虫(1)——BeautifulSoup库函数find_all()
- python - BeautifulSoup中的find()和findAll()
- BeautifulSoup的高级应用 之 find findAll
- BeautifulSoup的高级应用 之 find findAll
- BeautifulSoup库中find_all()方法
- BeautifulSoup库之find、findAll和children、descendants
- python爬虫之BeautifulSoup的用法
- BeautifulSoup使用find_all方法乱码问题
- python BeautifulSoup 的安装和使用
- python的urllib2和beautifulsoup编写爬虫
- Windows 启动 Eclipse启动出现:a java runtime environment(JRE) or java development kit(JDK) must be....
- java多线程基本知识体系
- 发表3308篇论文是一种怎样的体验?并非搬运自知乎
- fabric.js自定义字体的引入
- CentOS 7.3下安装MySQL 5.7
- Python的BeautifulSoup之find和find_all
- 小白的Python之路——初识Python
- MySQL多表查询
- Mybatis单个参数的if判断(针对异常:There is no getter for property..)
- 马里奥吃金币消失
- Unity Profiler 性能分析
- Kafka Java 牛刀小试
- C语言实现直方图统计和直方图均衡化
- android studio 3.0 升级问题,遇到的坑;