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

Python + selenium + phantomjs 求助,爬一个网站的信息

  •  
  •   doulmi · 2017-02-26 21:53:43 +08:00 · 2585 次点击
    这是一个创建于 2608 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近公司要求爬下 http://www.kuajingjishi.com/Purchase/SearchPurchase#pageIndex=10 页面的采购信息,本以为会是比较简单的事情,结果每次爬下来都是第一页的内容

    from selenium import webdriver
    
    driver = webdriver.PhantomJS()
    driver.get('http://www.kuajingjishi.com/Purchase/SearchPurchase#pageIndex=2')
    result = driver.find_element_by_tag_name('table')
    print(result.get_attribute('innerHTML'))
    

    求助求助~

    6 条回复    2017-02-27 10:32:01 +08:00
    aploium
        1
    aploium  
       2017-02-26 22:02:57 +08:00
    ```python
    import requests

    data = {
    'X-Requested-With': 'XMLHttpRequest'
    }

    for i in range(1,10):
    url='http://www.kuajingjishi.com/Purchase/SearchPurchase?pageIndex={page}'.format(page=i)
    r=requests.post(url, data=data)
    print(r.text)
    ```
    aploium
        2
    aploium  
       2017-02-26 22:17:25 +08:00
    它是用 ajax 加载的, 浏览器里用 F12, 选 network 那个 tab, 手动点一下那几个页面, 能看到它发出去的请求, 然后自己一模一样发好了
    这个站还没对请求进行验证......orz

    至于为什么 selenium 弄不到......天知道:)

    tips: 在请求上右键->copy->copy as cURL 然后在这个网站能直接转换为 requests 格式 https://curl.trillworks.com/
    doulmi
        3
    doulmi  
    OP
       2017-02-26 23:39:58 +08:00
    @aploium 谢谢你!
    mingyun
        4
    mingyun  
       2017-02-26 23:44:20 +08:00
    @aploium 这个网站好
    MyFaith
        5
    MyFaith  
       2017-02-27 08:38:03 +08:00
    pageIndex:9
    POST 的时候把这个参数加上就好了,只是一个 AJAX 加载
    cranelee13
        6
    cranelee13  
       2017-02-27 10:32:01 +08:00
    python 爬虫只有到完全没办法,反爬实在厉害才用 selenium + phantomjs ,绝大多数情况用 requests 应付就行。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3671 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 10:26 · PVG 18:26 · LAX 03:26 · JFK 06:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.