Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在beautifulSoup中按位置选择多个标记_Python_Beautifulsoup - Fatal编程技术网

Python 在beautifulSoup中按位置选择多个标记

Python 在beautifulSoup中按位置选择多个标记,python,beautifulsoup,Python,Beautifulsoup,考虑以下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 th

考虑以下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(“类型的第n个([2,3,5]))
选择(“类型(2,3,5)的第n个”)

如果你的解决方案不是围绕CSS类,你最好使用
soup.find_all()


使用带有逗号的CSS选择器“
”,“
“a:n子项(2)、a:n子项(3)、a:n子项(5)”

导入请求
从bs4导入BeautifulSoup
html_doc=“”
睡鼠的故事
睡鼠的故事

从前有三个小姐妹,她们的名字是 , 和 ; 他们住在井底

""" soup=BeautifulSoup(html_doc,'html.parser') a2、a3、a5=汤。选择('a:n个孩子(2),a:n个孩子(3),a:n个孩子(5)' 印刷品(a2) 印刷品(a3) 印刷品(a5)
印刷品:

<a class="sister" href="http://example.com/tillie" id="link3">Millie</a>
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>



更多信息:

这不起作用,我在扫描字符串文字错误时得到一个下线
import requests
from bs4 import BeautifulSoup

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 = BeautifulSoup(html_doc, 'html.parser')

a2, a3, a5 = soup.select('a:nth-child(2), a:nth-child(3), a:nth-child(5)')

print(a2)
print(a3)
print(a5)
<a class="sister" href="http://example.com/tillie" id="link3">Millie</a>
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>