作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做网络抓取,但我在 find() 和 find_all() 中卡住/困惑。
比如在哪里使用find_all,在哪里使用find()。
另外,我可以在哪里使用这种方法,例如 for 循环 或在 ul li 列表 ??
这是我试过的代码
from bs4 import BeautifulSoup
import requests
urls = "https://www.flipkart.com/offers-list/latest-launches?screen=dynamic&pk=themeViews%3DAug19-Latest-launch-Phones%3ADTDealcard~widgetType%3DdealCard~contentType%3Dneo&wid=7.dealCard.OMU_5&otracker=hp_omu_Latest%2BLaunches_5&otracker1=hp_omu_WHITELISTED_neo%2Fmerchandising_Latest%2BLaunches_NA_wc_view-all_5"
source = requests.get(urls)
soup = BeautifulSoup(source.content, 'html.parser')
divs = soup.find_all('div', class_='MDGhAp')
names = divs.find_all('a')
full_name = names.find_all('div', class_='iUmrbN').text
print(full_name)
File "C:/Users/ASUS/Desktop/utube/sunil.py", line 9, in <module>
names = divs.find_all('a')
File "C:\Users\ASUS\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bs4\element.py", line 1601, in __getattr__
raise AttributeError(
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()- 只在页面中找到搜索到的元素时返回结果。返回类型为<class 'bs4.element.Tag'>
.
find_all() - 它返回所有匹配项(即它扫描整个文档并返回所有结果,返回类型将为 <class 'bs4.element.ResultSet'>
)
from robobrowser import RoboBrowser
browser = RoboBrowser(history=True)
browser = RoboBrowser(parser='html.parser')
browser.open('http://www.stackoverflow.com')
res=browser.find('h3')
print(type(res),res)
print(" ")
res=browser.find_all('h3')
print(type(res),res)
print(" ")
print("Iterating the Resultset")
print(" ")
for x in range(0,len(res)):
print(x,res[x])
print(" ")
<class 'bs4.element.Tag'> <h3><a href="https://stackoverflow.com">current community</a>
</h3>
<class 'bs4.element.ResultSet'> [<h3><a href="https://stackoverflow.com">current community</a>
</h3>, <h3>
your communities </h3>, <h3><a href="https://stackexchange.com/sites">more stack exchange communities</a>
</h3>, <h3 class="w90 mx-auto ta-center p-ff-roboto-slab-bold fs-headline2 mb24">Questions are everywhere, answers are on Stack Overflow</h3>, <h3 class="w90 mx-auto ta-center p-ff-roboto-slab-bold fs-headline2 mb24">Learn and grow with Stack Overflow</h3>, <h3 class="mx-auto w90 wmx12 p-ff-roboto-slab-bold fs-headline2 mb24 lg:ta-center">Looking for a job?</h3>]
Iterating the Resultset
0 <h3><a href="https://stackoverflow.com">current community</a>
</h3>
1 <h3>
your communities </h3>
2 <h3><a href="https://stackexchange.com/sites">more stack exchange communities</a>
</h3>
3 <h3 class="w90 mx-auto ta-center p-ff-roboto-slab-bold fs-headline2 mb24">Questions are everywhere, answers are on Stack Overflow</h3>
4 <h3 class="w90 mx-auto ta-center p-ff-roboto-slab-bold fs-headline2 mb24">Learn and grow with Stack Overflow</h3>
5 <h3 class="mx-auto w90 wmx12 p-ff-roboto-slab-bold fs-headline2 mb24 lg:ta-center">Looking for a job?</h3>
关于python - 美汤python中的find()和find_all()有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59780916/
日前,阿里数娱发布了新一代互联网机顶盒天猫魔盒3pro,今天已正式首发开卖,售价299元。 现在,我们已经拿到这款产品,下面为大家带来图赏。 外观方面,天猫魔盒3pro更加小巧轻薄,采用经典的黑
我是一名优秀的程序员,十分优秀!