Python-如何使用BeautifulSoup将另一个类中的类作为目标?

Python-如何使用BeautifulSoup将另一个类中的类作为目标?,python,beautifulsoup,web-crawler,Python,Beautifulsoup,Web Crawler,我正在学习使用beautifulsoup和Python 3创建爬虫程序,我遇到了一个问题,我想在网站中获取的数据有多个类,下面是一个示例: <tr class="phone"> <a href="..." class="number"></a> </tr> <tr class="mobile"> <a href="..." class="number"></a> </tr> 我应该如何

我正在学习使用beautifulsoup和Python 3创建爬虫程序,我遇到了一个问题,我想在网站中获取的数据有多个类,下面是一个示例:

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

<tr class="mobile">
  <a href="..." class="number"></a>
</tr> 
我应该如何定位class.mobile.number?

找到class number的所有元素,然后遍历列表并打印其父类为mobile的元素

for dom in soup.find_all("a", "number"):
    # this returns a list of class names
    for class in dom.parent()["class"]:     
    if class == "mobile":
        print(dom.string)
或者使用“选择”作为CSS选择器样式

for dom in soup.select("tr.mobile a.number"):
    print(dom.string)
您可以使用来根据查找项目


最简单的方法是首先获取所有移动字段,然后对其运行选择器以查找数字。我想知道dictionary如何成为CSS选择器,但您可以尝试:{'class':'mobile>number'}为什么会出现此错误:TypeError:列表索引必须是整数,not str,使用select选项时,不会打印任何内容:/@Sia my mission dom.parent['class']实际上会返回一个列表,因为元素可以有许多类,因此需要另一个循环。我看不出select代码有任何问题。>和“”CSS选择器的区别是div>p将只选择作为div的直接子级的p,而div p将选择div中的所有p,包括孙子。然而,如果你的HTML和你发布的一样,>不起作用,这很奇怪。
for dom in soup.select("tr.mobile a.number"):
    print(dom.string)
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