对脱机文件运行xpath查询



我已经抓取了一个网页的完整html,并将其保存为information.txt

有没有一个简单的方法(如果有的话(可以对这个本地保存的txt文件运行xpath查询我不希望对在线网页运行xpath查询(由于各种原因(。

到目前为止,我的代码是;

from lxml import html
import requests
file = open('information.txt' , 'r')
file.seek(0)
target=file.read()
file.close
data1 = target.xpath('/html/body/p')

但收到的错误信息是;

Traceback (most recent call last):
File "getxpath.py", line 15, in <module>
data1 = target.xpath('/html/body/p')
AttributeError: 'str' object has no attribute 'xpath'

R解决方案。使用htmlParse解析文件,并使用xpathSapply请求。函数随包XML一起提供。

最新更新