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() 方法.下面两行代码是等价的:

soup.find_all('title', limit=1)# [<title>The Dormouse's story</title>]soup.find('title')# <title>The Dormouse's story</title>

唯一的区别是 find_all() 方法的返回结果是值包含一个元素的列表,而 find() 方法直接返回结果.

find_all() 方法没有找到目标是返回空列表, find() 方法找不到目标时,返回 None .


红色字部分是重点,这也就能解释为什么用find_all之后再用find_all会报错,因为find_all返回的是一个list,再对list用find_all时,需要指定元素,所以,改为:
tt=soup.body.find_all('ul',class_= 't clearfix')[0]
 
阅读全文
0 0