从类'requests_html.Element'中提取值



我使用选择器成功地向网站发送了请求

request.html.find(#selector(.

这将返回一个长度为1的列表数组,然后我可以使用offernew[0]从列表中提取该数组,其中offernew是在html.find查询之后返回的内容。

这返回<class 'requests_html.Element'>类型的内容,并包含

<Element 'input' type='hidden' name='ThisIsWhatIWant' value='XXXXXYYYY'>

我正在尝试提取ThisIsWhatIWant值,但是我真的被卡住了。据我所知,这个值不能像字典或列表一样提取,所以有人能提出建议吗?

如有任何帮助,我们将不胜感激。

第一页的requests_html文档显示

Introspect an Element’s attributes: 
>>> about.attrs 
{'id': 'about', 'class': ('tier-1', 'element-1'), 'aria-haspopup': 'true'}

这是你的答案

html.find(...)[0].attrs{'name']

为学习而专门创建的具有真实URL的最小工作代码

import requests_html
s = requests_html.HTMLSession()
r = s.get('http://books.toscrape.com/')
for item in r.html.find('h3 a'):
print('title:', item.attrs['title'])

最新更新