【发布时间】:2018-12-21 07:51:35
【问题描述】:
我正在尝试提取 cpu 的套接字类型,如您在以下image 中所见。我已经确定套接字类型位于<h4> Socket 标题下,如下面的image 所示。
到目前为止,我已经能够抓取 .spec.block 并找到所有嵌套在里面的 <h4>'s。但是我无法获取每个标题下的文字
这是我的代码
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://au.pcpartpicker.com/product/' + jLF48d)
about = r.html.find('.specs.block')[0]
about = about.find('h4')
print(about.text)
打印出来
[ <Element 'h4' >, <Element 'h4' >, <Element 'h4' >, <Element 'h4' >,
<Element 'h4' >, <Element 'h4' >, <Element 'h4' >, <Element 'h4' >,
<Element 'h4' >, <Element 'h4' >, <Element 'h4' >]
但是,当我将打印语句更改为:
print(about.text)
我收到以下错误:
AttributeError: 'list' 对象没有属性 'text'
更新:
print(about[0].text)
此代码打印:
制造商 AMD 这是第一个标题和文本,但我需要第四个
知道我可以使用什么代码来达到预期的结果吗?
如果您需要更多信息,请告诉我。
【问题讨论】:
-
about[0].text -
返回第一个标题和文本:制造商 AMD。如何获得第 4 个标题和文本套接字?谢谢
-
修复了关于[3].text
标签: python html python-3.x python-requests-html