python爬虫中get和post方法介绍以及cookie作用

栏目: 编程语言 · Python · 发布时间: 6年前

内容简介:本篇文章通过爬取163邮箱实例介绍了python爬虫中get和post方法介绍以及cookie作用,对此有兴趣的朋友学习下。

首先确定你要爬取的目标网站的表单提交方式,可以通过开发者 工具 看到。这里推荐使用chrome。

这里我用163邮箱为例

打开工具后再Network中,在Name选中想要了解的网站,右侧headers里的request method就是提交方式。status如果是200表示成功访问下面的有头信息,cookie是你登录之后产生的存储会话(session)信息的。第一次访问该网页需要提供用户名和密码,之后只需要在headers里提供cookie就可以登陆进去。

引入requests库,会提供get和post的方法。

代码示例:

import requests
import ssl

user_agent="Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0"
accept='text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
accept_language='zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3'
upgrade='1'
headers={
  'User-Agent':user_agent,
  'Accept':accept,
  'Accept-Language':accept_language,
'Cookie':'....'#这里填入你登陆后产生的cookie
}
r = requests.get("http://mail.163.com/js6/main.jsp?sid=OAwUtGgglzEJoANLHPggrsKKAhsyheAT&df=mail163_letter#module=welcome.WelcomeModule%7C%7B%7D",headers=headers,verify=False)
fp = open("/temp/csdn.txt","w",encoding='utf-8')
fp.write(str(r.content,'utf-8'))
fp.close()

这里我引入了ssl库,因为我第一次访问的网页证书过期。如果我们使用爬虫进入这样的网站时,会报错:SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)

在requests的get和post方法中,有一个参数为verify,把他设为False后会禁掉证书的要求


以上所述就是小编给大家介绍的《python爬虫中get和post方法介绍以及cookie作用》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Art of Computer Programming, Volume 2

The Art of Computer Programming, Volume 2

Knuth, Donald E. / Addison-Wesley Professional / 1997-11-04 / USD 79.99

Finally, after a wait of more than thirty-five years, the first part of Volume 4 is at last ready for publication. Check out the boxed set that brings together Volumes 1 - 4A in one elegant case, and ......一起来看看 《The Art of Computer Programming, Volume 2》 这本书的介绍吧!

html转js在线工具
html转js在线工具

html转js在线工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具