use*_*884 7 python beautifulsoup web-scraping
让我们说:
<div>
<p>this is some text</p>
<p>...and this is some other text</p>
</div>
Run Code Online (Sandbox Code Playgroud)
如何从beautifulsoup的第二段中检索文本?
sty*_*ane 14
您可以使用CSS选择器执行此操作:
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup("""<div>
.... <p>this is some text</p>
.... <p>...and this is some other text</p>
.... </div>""", "html.parser")
>>> soup.select('div > p')[1].get_text(strip=True)
'...and this is some other text'
Run Code Online (Sandbox Code Playgroud)
Pad*_*ham 11
你可以使用nth-of-type:
h = """<div>
<p>this is some text</p>
<p>...and this is some other text</p>
</div>"""
soup = BeautifulSoup(h)
print(soup.select_one("div p:nth-of-type(2)").text)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12147 次 |
| 最近记录: |