V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
snowsie
V2EX  ›  Python

Python 爬虫问题请教各位

  •  
  •   snowsie · 2017-05-03 13:00:47 +08:00 · 2241 次点击
    这是一个创建于 2551 天前的主题,其中的信息可能已经有所发展或是发生改变。
    刚接触了些爬虫知识,想爬一个网站部分信息,目前环境 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 线")

    怎么才能获取此元素并点击它呢,谢谢各位帮助!!
    8 条回复    2017-05-03 15:38:04 +08:00
    VicYu
        1
    VicYu  
       2017-05-03 13:09:24 +08:00
    金融数据,拿到基础数据,自己算吧,API 一堆堆
    snowsie
        2
    snowsie  
    OP
       2017-05-03 13:19:10 +08:00
    只是以这个为示例,只想知道遇到这种问题的思路,谢谢
    Shazoo
        3
    Shazoo  
       2017-05-03 13:21:10 +08:00
    看了一眼。这个数据段在一个 iframe 里面的。你取的数据 url 改改试试。
    Shazoo
        4
    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
        5
    snowsie  
    OP
       2017-05-03 13:34:23 +08:00
    测试成功 谢谢你!!
    snowsie
        6
    snowsie  
    OP
       2017-05-03 15:15:13 +08:00
    @Shazoo 获取了这个网址后 如果我想要获取 MA5,MA10 后的数字,该怎么获取呢?多谢在帮我看看吧
    snowsie
        7
    snowsie  
    OP
       2017-05-03 15:25:56 +08:00
    我大概知道怎么弄了 还是谢谢啦
    dedura
        8
    dedura  
       2017-05-03 15:38:04 +08:00   ❤️ 1
    >>> 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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2947 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:10 · PVG 17:10 · LAX 02:10 · JFK 05:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.