gpt4 book ai didi

python - python中通过xpath提取A标签的值

转载 作者:行者123 更新时间:2023-12-01 05:01:28 25 4
gpt4 key购买 nike

我有一个简单的 python 脚本,例如:

#!/usr/bin/python
import requests
from lxml import html
response = requests.get('http://site.ir/')
out=response.content
tree = html.fromstring(open(out).read())
print [e.text_content() for e in tree.xpath('//div[class="group"]/div[class="groupinfo"]/a/text()')]

我使用 xpath 来获取标签 a 的值,如下图所示... enter image description here但输出样本不是我所期望的。

更新我也有以下错误:

Traceback (most recent call last):
File "p.py", line 7, in <module>
tree = html.fromstring(open(out).read())
IOError: [Errno 36] File name too long: '\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ....

最佳答案

您需要将 @ 放在属性名称的开头以在 XPath 中寻址属性:

//div[@class="group"]/div[@class="groupinfo"]/a/text()

关于python - python中通过xpath提取A标签的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26038039/

25 4 0