Answer the question
In order to leave comments, you need to log in
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')
Answer the question
In order to leave comments, you need to log in
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
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
# []
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question