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
JianBingXia
V2EX  ›  Python

使用 webdriver.PhantomJS 无法加载 ajax 吗?

  •  
  •   JianBingXia · 2016-11-29 16:41:57 +08:00 · 3011 次点击
    这是一个创建于 2676 天前的主题,其中的信息可能已经有所发展或是发生改变。
    driver = webdriver.PhantomJS()
    driver.get(url)
    data = driver.find_elements_by_xpath('***********')

    试了试,貌似 js 生成的动态网页可以加载正常,但为什么 ajax 还是没有加载出来呢?应该怎么处理呢?

    另外 page_source 其实是未加 js 的原始 html 吧?就和 chrome 查看网页源代码一样吧?
    那么想和 F12 看到的一样用什么方法呢?

    感谢.
    4 条回复    2016-11-30 23:40:55 +08:00
    SilentDepth
        1
    SilentDepth  
       2016-11-29 18:39:51 +08:00   ❤️ 1
    不了解 webdriver ,但 PhantomJS 是可以处理 AJAX 的。
    Chrome 的查看网页源代码功能和 F12 是一个效果啊
    bdbai
        2
    bdbai  
       2016-11-29 19:43:15 +08:00 via Android   ❤️ 1
    等几秒试试?
    F12 Document 查看的是当前网页的 DOM ,被 js 改过的,不跑一遍没法一样。
    简单的请求可以从 F12 Network 里面抓。模拟发请求比 PhantomJS 效率高。
    pixes4world
        3
    pixes4world  
       2016-11-29 20:08:01 +08:00   ❤️ 1
    不管你用什么 webdriver , ajax 加载都需要等待的,最直接的方法就是指定固定的等待时间: time.sleep(10) ;
    或者智能一点,等待特定元素加载完成: WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located(locator));
    mingyun
        4
    mingyun  
       2016-11-30 23:40:55 +08:00
    @pixes4world 可行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5303 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 09:33 · PVG 17:33 · LAX 02:33 · JFK 05:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.