Python 爬虫问题请教各位

2017-05-03 13:00:47 +08:00
 snowsie
刚接触了些爬虫知识,想爬一个网站部分信息,目前环境 python2.7+selenium3+phantomjs

我想模拟浏览器点击这个网址的日 K 线,但是 driver.get 出来后找不到该元素, 好像都是 js 代码。请问各位如何获取到这个元素并点击

from selenium import webdriver
driver=webdriver.PhantomJS()
driver.get("http://stockpage.10jqka.com.cn/000791/")
print driver.page_source #此处打印出很多 js 代码 和浏览器的 html 代码不同, 希望各位高手解释
driver.find_element_by_xpath("//li[@period='daily']/a").click() #报错 提示这个异常 selenium.common.exceptions.NoSuchElementException: 但是在浏览器的 html 里好像是这样的
# driver.find_element_by_link_text("日 K 线")

怎么才能获取此元素并点击它呢,谢谢各位帮助!!
2248 次点击
所在节点    Python
8 条回复
VicYu
2017-05-03 13:09:24 +08:00
金融数据,拿到基础数据,自己算吧,API 一堆堆
snowsie
2017-05-03 13:19:10 +08:00
只是以这个为示例,只想知道遇到这种问题的思路,谢谢
Shazoo
2017-05-03 13:21:10 +08:00
看了一眼。这个数据段在一个 iframe 里面的。你取的数据 url 改改试试。
Shazoo
2017-05-03 13:23:55 +08:00
>>> driver.get("http://stockpage.10jqka.com.cn/HQ_v3.html#hs_000791")
>>> driver.find_element_by_xpath("//li[@period='daily']").click()
snowsie
2017-05-03 13:34:23 +08:00
测试成功 谢谢你!!
snowsie
2017-05-03 15:15:13 +08:00
@Shazoo 获取了这个网址后 如果我想要获取 MA5,MA10 后的数字,该怎么获取呢?多谢在帮我看看吧
snowsie
2017-05-03 15:25:56 +08:00
我大概知道怎么弄了 还是谢谢啦
dedura
2017-05-03 15:38:04 +08:00
>>> driver.get("http://stockpage.10jqka.com.cn/HQ_v3.html#hs_000791")
>>> driver.find_element_by_xpath("//li[@period='daily']").click()

然后要先移动到曲线上才能出现这个 ma 信息

from selenium.webdriver.common.action_chains import ActionChains

a=driver.find_element_by_xpath('//*[@id="canvasPanel"]')

ActionChains(driver).move_to_element(a).perform()

driver.find_element_by_xpath('//*[@id="kTipma"]').text

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/358770

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX