gpt4 book ai didi

python - 使用 Python 抓取 HTML,document_fromstring 为空

转载 作者:行者123 更新时间:2023-12-03 21:37:04 25 4
gpt4 key购买 nike

我正在尝试使用 python 从网站中提取一些数据。我找到了一个 ( document 完全适合我的问题。

但是当我运行提供的代码时

from lxml import html
import requests


page = requests.get('http://econpy.pythonanywhere.com/ex/001.html')
tree = html.fromstring(page.content)

#This will create a list of buyers:
buyers = tree.xpath('//div[@title="buyer-name"]/text()')
#This will create a list of prices
prices = tree.xpath('//span[@class="item-price"]/text()')


print 'Buyers: ', buyers
print 'Prices: ', prices

我收到一个错误:
  File "C:\Python27\lib\site-packages\lxml\html\__init__.py", line 617, in document_fromstring
"Document is empty")

ParserError: Document is empty

任何人都知道问题可能是什么?

最佳答案

你的脚本对我来说很好。我得到了输出:

Buyers:  ['Carson Busses', 'Earl E. Byrd', 'Patty Cakes', 'Derri Anne Connecticut', 'Moe Dess', 'Leda Doggslife', 'Dan Druff', 'Al Fresco', 'Ido Hoe', 'Howie Kisses', 'Len Lease', 'Phil Meup', 'Ira Pent', 'Ben D. Rules', 'Ave Sectomy', 'Gary Shattire', 'Bobbi Soks', 'Sheila Takya', 'Rose Tattoo', 'Moe Tell']
Prices: ['$29.95', '$8.37', '$15.26', '$19.25', '$19.25', '$13.99', '$31.57', '$8.49', '$14.47', '$15.86', '$11.11', '$15.98', '$16.27', '$7.50', '$50.85', '$14.26', '$5.68', '$15.00', '$114.07', '$10.09']

我建议你试试 latest lxml package .并检查 desired webpage您现在可以使用。

关于python - 使用 Python 抓取 HTML,document_fromstring 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34306600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com