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

关于 Request 和 webdriver 的问题请教

  •  
  •   saximi · 2017-09-20 20:35:50 +08:00 · 2578 次点击
    这是一个创建于 2416 天前的主题,其中的信息可能已经有所发展或是发生改变。
    1、在爬取网页时,Scrapy.Request 和 Selenium.webdriver 是不是只能二选一?
    我既需要用 Request 来设置请求头,也希望将 webdriver 和 PhantomJS 结合在一起使用,Request 和 webdriver 同时使用可行么,如果可行,是否会出现 Request 和 webdriver 分别去加载同一个网页,即一个页面至少加载两次?
    不知有没有两者结合起来使用的范例。

    2、用 webdriver 的 get()方法加载多个页面,例如 get(url1)、get(url2)等,请问 close()方法是在每次 get 后都要执行,还是在整个程序的末尾执行一次即可?
    谢谢!
    3 条回复    2017-09-21 01:23:53 +08:00
    punderson
        1
    punderson  
       2017-09-20 22:27:01 +08:00
    1,你直接修改 middleware,针对某些链接用 driver,某些用 request。这样的话 scrapy 就会不会重复抓取。2,close()是相当于你关闭一个 tab,quit()则是退出浏览器。如果你在同一个 tab 里面不断打开 url,不知道会不会导致内存占用不大。没试验过
    saximi
        2
    saximi  
    OP
       2017-09-20 22:59:31 +08:00
    @punderson 谢谢,因为我需要设置请求头,所以每次访问我都不得不用 request ( webdriver 应该没有设置请求头的功能吧?) ,但是同时我又希望使用 webdriver 的功能来访问页面,这种需求是不是就导致我不得不对于每个页面都要用 request 和 webdriver 分别加载了?
    holajamc
        3
    holajamc  
       2017-09-21 01:23:53 +08:00
    说到 webdriver 和 headers,至少 PhantomJS 是可以自定义的 http://phantomjs.org/api/webpage/property/custom-headers.html

    说到 webdriver 和 Scrapy,肯定是可以结合的 https://github.com/brandicted/scrapy-webdriver
    鉴于 PhantomJS 的效率,可能需要使用一些参数来修补
    使用缓存 --disk-cache=yes
    不加载图片 --load-images=no
    忽略 ssl error --ignore-ssl-errors=true

    或者直接使用 Chrome Headless
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2267 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 09:35 · PVG 17:35 · LAX 02:35 · JFK 05:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.