漂亮的汤蟒中的find()和find_all()有什么区别?

问题描述 投票:0回答:3

我正在进行网页抓取,但是我在find()和find_all()中卡住/感到困惑。

像在哪里使用find_all,在哪里使用find()。

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

python python-3.x web-scraping beautifulsoup python-requests
3个回答
0
投票

用这个例子也许更清楚了:

from bs4 import BeautifulSoup
import re

html = """
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
"""   
soup = BeautifulSoup(html,'html.parser')

for n in soup.find('li'):
  # It Give you one element     
  print(n)

for n in soup.find_all('li'):    
  # It Give you all elements
  print(n)

结果:

First

<li>First</li>
<li>Second</li>
<li>Third</li>

有关更多信息,请阅读此https://www.crummy.com/software/BeautifulSoup/bs4/doc/#calling-a-tag-is-like-calling-find-all


0
投票

从Beautiful Soup文档中找到。如果要抓取更具体的内容,请尝试find;如果要抓取更普通的内容aspan,则可以尝试find_all。https://www.crummy.com/software/BeautifulSoup/bs4/doc/

soup.find_all('a')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#  <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
#  <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

soup.find(id="link3")
# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

希望这会有所帮助!


0
投票

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>
© www.soinside.com 2019 - 2024. All rights reserved.