简体   繁体   English

如何使用新的 Requests-HTML 库查找特定类型的所有元素

[英]How to find all Elements of a specific Type with the new Requests-HTML library

I wanna find all specific fields in a HTML, in Beautiful soup everything is working with this code:我想在 HTML 中找到所有特定字段,在 Beautiful Soup 中,一切都在使用以下代码:

soup = BeautifulSoup(html_text, 'html.parser')
urls_previous = soup.find_all('h2', {'class': 'b_algo'})

but how can I make the same search with the requests library or can requests only find a single element in a HTML document, I couldn't find how to do it in the docs or examples ?但是如何使用请求库进行相同的搜索,或者请求只能在 HTML 文档中找到单个元素,我在文档或示例中找不到如何执行此操作?

https://html.python-requests.org/ https://html.python-requests.org/

Example:例子:

 <li class="b_algo"><h2><a href="https://de.wikipedia.org/wiki/Vereinigte_Staaten">Vereinigte Staaten – Wikipedia</a></h2><a href="https://de.wikipedia.org/wiki/Vereinigte_Staaten">https://de.wikipedia.org/wiki/Vereinigte_Staaten</a></div><p>US, I wanna have THIS text here</p></li>

How can I find all Elements of a specific type with the requests library ?如何使用请求库找到特定类型的所有元素?

with requests-html带有请求-html

from requests_html import HTML
doc = """<li class="b_algo"><h2><a href="https://de.wikipedia.org/wiki/Vereinigte_Staaten">Vereinigte Staaten – Wikipedia</a></h2><a href="https://de.wikipedia.org/wiki/Vereinigte_Staaten">https://de.wikipedia.org/wiki/Vereinigte_Staaten</a></div><p>U.S., I wanna have THIS text here</p></li>"""
#load html from string
html = HTML(html=doc)
x = html.find('h2')
print(x)

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

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