V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Feb 26, 2017 · 3382 views
    This topic created in 3348 days ago, the information mentioned may be changed or developed.

    最近公司要求爬下 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 replies    2017-02-27 10:32:01 +08:00
    aploium
        1
    aploium  
       Feb 26, 2017
    ```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  
       Feb 26, 2017
    它是用 ajax 加载的, 浏览器里用 F12, 选 network 那个 tab, 手动点一下那几个页面, 能看到它发出去的请求, 然后自己一模一样发好了
    这个站还没对请求进行验证......orz

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

    tips: 在请求上右键->copy->copy as cURL 然后在这个网站能直接转换为 requests 格式 https://curl.trillworks.com/
    doulmi
        3
    doulmi  
    OP
       Feb 26, 2017
    @aploium 谢谢你!
    mingyun
        4
    mingyun  
       Feb 26, 2017
    @aploium 这个网站好
    MyFaith
        5
    MyFaith  
       Feb 27, 2017
    pageIndex:9
    POST 的时候把这个参数加上就好了,只是一个 AJAX 加载
    cranelee13
        6
    cranelee13  
       Feb 27, 2017
    python 爬虫只有到完全没办法,反爬实在厉害才用 selenium + phantomjs ,绝大多数情况用 requests 应付就行。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5647 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 07:24 · PVG 15:24 · LAX 00:24 · JFK 03:24
    ♥ Do have faith in what you're doing.