今天学习写爬虫,练习网址为http://blog.csdn.net/bo_wen_/article/details/50868339,做一个抓取每日最高最低温度的练习。在过程中遇到这样一个问题,代码所示:
# coding : UTF-8
import requests
from bs4 import BeautifulSoup
res = 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)
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,尽管有时候我们只想得到一个结果.比如文档中只有一个标签,那么使用 find_all() 方法来查找标签就不太合适, 使用 find_all 方法并设置 limit=1 参数不如直接使用 find() 方法.下面两行代码是等价的:
soup.find_all('title', limit=1)
# [The Dormouse's story ]
soup.find('title')
# The Dormouse's story
唯一的区别是 find_all() 方法的返回结果是值包含一个元素的列表,而 find() 方法直接返回结果.
find_all() 方法没有找到目标是返回空列表, find() 方法找不到目标时,返回 None .
tt=soup.body.find_all('ul',class_= 't clearfix')[0]