简体   繁体   English

根据“美丽汤”中的文本选择元素

[英]Select element based on text inside Beautiful Soup

I scrapped a website and I want to find an element based on the text written in it. 我报废了一个网站,但我想根据其中的文字查找一个元素。 Let's say below is the sample code of the website: 假设以下是该网站的示例代码:

code = bs4.BeautifulSoup("""<div>
<h1>Some information</h1>
<p>Spam</p>
<p>Some Information</p>
<p>More Spam</p>
</div>""")

I want some way to get a p element that has as a text value Some Information . 我想要某种方式来获取具有文本值Some Informationp元素。 How can I select an element like so? 如何选择这样的元素?

Just use text parameter: 只需使用text参数:

code.find_all("p", text="Some Information")

If you need only the first element than use find instead of find_all . 如果只需要第一个元素,则使用find而不是find_all

You could use text to search all tags matching the string 您可以使用text搜索与字符串匹配的所有标签

import BeautifulSoup as bs
import re
code = bs.BeautifulSoup("""<div>
<h1>Some information</h1>
<p>Spam</p>
<p>Some Information</p>
<p>More Spam</p>
</div>""")


for elem in code(text='Some Information'):
    print elem.parent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM