gpt4 book ai didi

Python - 如何使用 BeautifulSoup 将一个类定位到另一个类中?

转载 作者:行者123 更新时间:2023-11-30 23:07:47 25 4
gpt4 key购买 nike

我正在学习用beautifulsoup和Python 3创建一个爬虫,我遇到了一个问题,我想要在网站中获取的数据有多个类,这里​​是一个例子:

<tr class="phone">
<a href="..." class="number"></a>
</tr>

<tr class="mobile">
<a href="..." class="number"></a>
</tr>
<小时/>

这就是我想用 Python 做的事情:

for num in soup.findAll('a', {'class':'mobile -> number'}):
print(num.string)

我应该如何定位 .mobile .number 类?

最佳答案

您可以使用soup.select根据 CSS selector 查找项目.

from bs4 import BeautifulSoup


html_doc = '''<tr class="phone">
<a href="tel:+18005551212" class="number"></a>
</tr>

<tr class="mobile">
<a href="+13034997111" class="number"></a>
</tr> '''

soup = BeautifulSoup(html_doc)

# Find any tag with a class of "number"
# that is a descendant of a tag with
# a class of "mobile"
mobiles = soup.select(".mobile .number")
print mobiles

# Find a tag with a class of "number"
# that is an immediate descendent
# of a tag with "mobile"
mobiles = soup.select(".mobile > .number")
print mobiles

# Find an <a class=number> tag that is an immediate
# descendent of a <tr class=mobile> tag.
mobiles = soup.select("tr.mobile > a.number")
print mobiles

关于Python - 如何使用 BeautifulSoup 将一个类定位到另一个类中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32054111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com