美汤python中的find()和find_all()有什么区别? [英] What is the difference between find() and find_all() in beautiful soup python?

查看:23
本文介绍了美汤python中的find()和find_all()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做网页抓取,但我在 find() 和 find_all() 中卡住/困惑.

I was doing web scraping but i stuck/confused in find() and find_all().

比如在哪里使用find_all,在哪里使用find().

Like where to use find_all, where to user find().

此外,我可以在哪里使用这种方法,例如在 for 循环ul li 列表中??

Also, where can i use this methods like in for loop or in ul li list ??

这是我试过的代码


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()?

那么谁能解释一下我应该在哪里使用 findfind all ??

So can anyone explain where should i use find and find all ??

推荐答案

find()- 只在页面中找到搜索到的元素时返回结果,返回类型为 <class 'bs4.element.Tag'>.

find()- It just returns the result when the searched element is found in the page.And the return type will be <class 'bs4.element.Tag'>.

find_all()- 它返回所有匹配项(即它扫描整个文档并返回所有结果,返回类型将为

find_all()- It returns all the matches (i.e) it scans the entire document and returns all the results and the return type will be <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中的find()和find_all()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆