2015-12-16 4 views
2

Я пытаюсь извлечь некоторые данные с сайта с помощью python. Я нашел (document, что точно соответствует моей проблемеHTML Скребок с Python, document_fromstring пуст

Но когда я запускаю предоставленный код

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 

Любой идею, что может быть проблема ?

+0

print 'page.content' или сохранить его в файле, посмотреть, содержит ли файл HTML. – har07

+0

попробуйте распечатать' page.text' – maazza

ответ

0

Ваш сценарий отлично работает для меня. Я получил на выходе:

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.

+0

После переустановки он работал :) Спасибо. – WitheShadow

Смежные вопросы