今天学习写爬虫,练习网址为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)
<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]
AttributeError: ResultSet object has no attribute ‘find_all’. You’re probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
意思是:属性错误:ResultSet对象没有属性’find_all’。您可能将一个元素列表当作一个元素来处理。当您打算调用find()时,是否调
学习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.pars...
AttributeError: ResultSet object has no attribute ‘find_all’. You’re probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
AttributeError: ResultSet object has no attribute 'find_all'. You're pro
标签表格标签(只要是展示数据 一般都可以使用表格标签)表单标签(重要:获取前端用户数据发送给后端)偷窥一下后端框架的基本使用(flask)css层贴样式表(选择器)表格标签jason 123 readegon123 dbjtank 123 hecha... 一个tr就表示一行username 加粗文本username 正常文本 表头(字段信息)jason123read 表单(数据信息) ...
好代码本身就是最好的文档。当你需要添加一个注释时,你应该考虑如何修改代码才能不需要注释。—— Steve McConnell目录字符串有很多操作函数,所以,这里我们专门用一节来介绍这些函数。建议:由于字符串函数较多,对于新手来说,不必要一开就掌握所有的函数用法,可以先粗略的看一遍,有个大概印象,到真正用的着的时候,再来详细查看也可。用的次数多了,自然就记住了。我们可以通过dir() 函数...
当我们在Chrome浏览器上,拿着url向服务器发出请求的时候,服务返回的是一个带有HTML文档的数据包,
经过浏览器解析,网页才能在窗口上正常呈现。
但是Python请求了远程服务器后,拿到的内容会是一份HTML文档
什么是HTML:
HTML是用来描述网页的一种语言,英文全称是Hyper Text Markup Language,也叫超文本标记语言。
基本格式:
<!DOCTYPE html>
1.一般来说,为了找到BeautifulSoup对象内任何第一个标签入口,使用find()方法。 以上代码是一个生态金字塔的简单展示,为了找到第一生产者,第一消费者或第二消费者,可以使用Beautiful Soup。找到第一生产者:生产者在第一个标签里,因为生产者在整个html文档中第一个标签中出现,所以可以使用find()方法找到第一生产者,在ecologicalpyramid.py中写入下面一...
find_all()
find_all,顾名思义,就是查询所有符合条件的元素。给它传入一些属性或文本,就可以得到符合条件的元素,返回结果是列表类型。
语法格式:find_all( name , attrs , recursive , text , **kwargs )
各个参数含义如下: