M
M
Mark Adams2016-10-01 19:36:10
Python
Mark Adams, 2016-10-01 19:36:10

How to deal with xpath in lxml Python?

Why the code doesn't work:

from lxml import html
import requests

page = requests.get('http://myip.ru')
tree = html.fromstring(page.content)

print tree.xpath('/html/body/div/div[1]/table/tbody/tr[2]/td')

Returns an empty sheet [].

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2016-10-01
@dimonchik2013

like this, it’s bad practice with numbers
, you need to be able to do
it without them secondly
and firstly, where are you in the view-source code:myip.ru DOM дерево нашел?
там же сплошной JS

S
Sergey, 2016-10-01
@sergnosov1

The output will be an empty string. content is loaded using jquery

import lxml.html as lh

page = lh.parse('http://myip.ru')
my_div = page.xpath(".//title")[0].text_content().encode('utf-8')
print my_div
# Ваш IP-адрес. Узнать IP адрес, определить IP-адрес, определить свой IP
div_name = 'ipcontent'
my_div = page.xpath(".//div[@id=%s]" %div_name)
print my_div
# []

чтобы получить результат работы jquery нужно использовать Selenium

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question