首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Selenium和lxml进行Python Web抓取

问使用Selenium和lxml进行Python Web抓取
EN

Stack Overflow用户
提问于 2016-12-30 20:22:13
回答 0查看 3.1K关注 0票数 2

我有个问题需要帮助。我正在尝试从一个网站上抓取一些数字(参见下面代码中的链接)。因为网站是使用JavaScript加载的,所以我使用selenium首先加载页面,然后将其传递给xlml以解析数据。

我使用的代码如下:

代码语言:javascript
复制
from selenium import webdriver
from lxml import html
import time

url = "http://sebgroup.com/large-corporates-and-institutions/prospectuses-and-downloads/rates/swap-rates"
xpath = '//*[@id="doc"]/table[2]/tbody/tr[3]/text()'

chrome_path = "my_path"
browser = webdriver.Chrome(chrome_path)
browser.get(url)
time.sleep(10)

html_source = browser.page_source

tree = html.fromstring(html_source)
text = tree.xpath(xpath)
print (text)

当我通过浏览器直接查看页面时,我可以看到源代码中的数字。但是当我使用selenium做同样的事情时,我看到的源代码是不同的。我想知道这是不是因为网站有一些防抓取软件?有没有办法仍然获得数据?(我需要它们用于学术用途)。

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41396202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档