<head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were &l […]">

python-在BeautifulSoup中按位置选择多个标签

2021-01-08 20点热度 0人点赞 0条评论

请考虑以下HTML:

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/tillie" class="sister" id="link3">Millie</a>
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""


 soup = bs4.BeautifulSoup(html_doc, 'html.parser')

如果我想要第二个A标签,我可以:

    soup.select("a:nth-of-type(2)")

但是,如果我想选择第二、第三和第五个A标签,我该怎么做呢?我尝试了下面的错误

    soup.select("a:nth-of-type([2, 3, 5])")
    soup.select("a:nth-of-type(2, 3, 5)")
查看隐藏内容需要支付:¥1
查看

未经允许不得转载!python-在BeautifulSoup中按位置选择多个标签

本文地址:https://ans.52learn.online/2156

ANS52LEARN

DO BEST