Python-硒和lxml中的相同xpath结果不同 [英] Python - Same xpath in selenium and lxml different results

查看:36
本文介绍了Python-硒和lxml中的相同xpath结果不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个网站 http://www.google-proxy.net/,我需要获取第一个代理的ip:port.

I have this site http://www.google-proxy.net/ and i need to get first proxy's ip:port.

br = webdriver.Firefox()
br.get("http://www.google-proxy.net/")
ip = br.find_element_by_xpath("//tr[@class='odd']/td[1]").text; time.sleep(random.uniform(1, 1))
port = br.find_element_by_xpath("//tr[@class='odd']/td[2]").text; time.sleep(random.uniform(1, 1))

,它工作正常.但是现在我想对lxml做同样的事情

and it works fine. But now i want to do the same with lxml

page = requests.get(proxy_server)
root = lxml.html.fromstring(page.text)
ip = root.xpath("//tr[@class='odd']/td[1]/text()")
port = root.xpath("//tr[@class='odd']/td[1]/text()")

,我得到了空列表.为什么会这样?

and i get empty lists. Why is that?

推荐答案

类似的"odd"类是通过Javascript在此站点中添加的.

Looks like 'odd' classes are added by Javascript in this site.

Selenium在运行浏览器时会执行Javascript,因此您具有预期的类.

Selenium, as it runs the browser, executes the Javascript, so you have the expected class.

请求库不会执行JS,因此没有"odd"类.

requests library will not execute JS, so there's no 'odd' class.

这篇关于Python-硒和lxml中的相同xpath结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆