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

Python 请求 cookie 的小疑问

  •  
  •   vipper · 2017-11-02 15:38:13 +08:00 · 1275 次点击
    这是一个创建于 2393 天前的主题,其中的信息可能已经有所发展或是发生改变。
    
    from urllib import request
    from http import cookiejar
    
    if __name__ == '__main__':
    	# 声明一个 CookieJar 对象实例来保存 cookie
    	cookie = cookiejar.CookieJar()
    	# 利用 urllib.request 库的 HTTPCookieProcessor 对象来创建 cookie 处理器,也就 CookieHandler
    	handler = request.HTTPCookieProcessor(cookie)
    	# 通过 CookieHandler 创建 opener
    	opener = request.build_opener(handler)
    	# 此处的 open 方法打开网页
    	response = opener.open('https://www.v2ex.com/')
    	# 打印 cookie 信息
    	for item in cookie:
    		print('Name = %s' % item.name)
    		print('Value = %s' % item.value)
            
    

    2 个疑问:

    一、利用上面的代码抓出的 cookie 可以是浏览器的中保存的吗?

    二、利用这种方法我在抓某个站的时候提示

    raise RemoteDisconnected("Remote end closed connection without" http.client.RemoteDisconnected: Remote end closed connection without response

    通过搜索引擎得知是服务器限制了爬虫,所以有没有什么办法伪造 User-Agent。我知道 request 的请求头可以伪

    造但是这个 CookieJar 的有些不理解。

    请各位大牛协助解决下问题,非常感谢

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4850 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 08:34 · PVG 16:34 · LAX 01:34 · JFK 04:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.