Найти - Пользователи
Полная версия: Проблема с lxml
Начало » Python для новичков » Проблема с lxml
1 2
bollustrado
Имеется код:
from lxml.html import parse
# Получаем страничку
page = parse('http://habrahabr.ru/').getroot()# Ищем все теги <a> с css классом topic
hrefs = page.cssselect("a.topic")
for row in hrefs:
# Получаем атрибут href
print(row.text)
Изначально он отлично работал, но после того, как я немного поэспериментировал, он выдает пустую строку. Попробовал:
from urllib.request import urlopen
from lxml import html
page = urlopen("http://habrahabr.ru/")
doc = html.document_fromstring(page.read())
for topic in doc.cssselect('a.topic'):
print(topic.text)
Результат тот же (пустая строка)
Пробовал переустанавливать python и lxml, не помогает. В список исключений брандмауэра python добавлен. Windows 7, Python 3.2, lxml 2.3.3.
Помогите плз, может кто сталкивался с таким?
s0rg
a.topic -> a.post_title
bollustrado
Пробовал переустанавливать python и lxml, не помогает.
А html посмотреть не проще?
bollustrado
Большое спасибо, я уже хотел винду сносить)
bollustrado
from lxml.html import parse
page = parse('http://pogoda.yandex.ru/moscow/').getroot()
hrefs = page.cssselect("div.b-forecast__tday")
for row in hrefs:
print(row.text)
Извините, а в этом коде что не так?
s0rg
Яндексу печеньки нужны:
from urllib2 import build_opener, HTTPHandler, HTTPCookieProcessor
from lxml.html import document_fromstring

browser = build_opener(HTTPHandler(), HTTPCookieProcessor())
html = browser.open('http://pogoda.yandex.ru/moscow/').read()
doc = document_fromstring(html)
for div in doc.cssselect("div.b-forecast__tday"):
print(div.text)
bollustrado
from urllib.request import build_opener, HTTPHandler, HTTPCookieProcessor
from lxml.html import document_fromstring

browser = build_opener(HTTPHandler(), HTTPCookieProcessor())
html = browser.open('http://pogoda.yandex.ru/moscow/').read()
doc = document_fromstring(html)
for div in doc.cssselect("div.b-forecast__tday"):
print(div.text)
выдает ошибку
HTTPError: HTTP Error 302: Found - Redirection to url
'moscow?ncrnd=107' is not allowed
s0rg
from urllib2 import build_opener, HTTPHandler, HTTPCookieProcessor, HTTPRedirectHandler
from lxml.html import document_fromstring

browser = build_opener(HTTPHandler(), HTTPCookieProcessor(), HTTPRedirectHandler())
html = browser.open('http://pogoda.yandex.ru/moscow/').read()
doc = document_fromstring(html)
for div in doc.cssselect("div.b-forecast__tday"):
print(div.text)
?
bollustrado
не помогает, та же ошибка.
s0rg
Возможно проблема в том, что я использую второй питон, а вы - третий
попробуйте заменить вот так:
from urllib.request import build_opener, HTTPHandler, HTTPCookieProcessor, HTTPRedirectHandler
Если не поможет - либо поставьте второй питон и попробуйте с ним (заменив ‘print(div.text)’ на ‘print div.text’)
Либо дождитесь кого-нибудь, кто поможет вам с третьей версией )
bollustrado
Спасибо
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB