Python中requests库的用法详解

 更新时间:2022年6月3日 21:50  点击:296 作者:springsnow

一、requests库

requests是使用Apache2 licensed 许可证的HTTP库。比urllib模块更简洁。

Request支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自动响应内容的编码,支持国际化的URL和POST数据自动编码。

在python内置模块的基础上进行了高度的封装,从而使得python进行网络请求时,变得人性化,使用Requests可以轻而易举的完成浏览器可有的任何操作。

安装

requests是第三方库,需要独立安装:pip install requests。requests是基于urllib编写的,并且使用起来非常方便,个人推荐使用requests。

官方中文教程地址:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html

学习之前推荐一个非常好的http测试网站:http://httpbin.org,提供非常非常完善的接口调试、测试功能~

请求

requests支持http的各种请求,比如:

  • GET: 请求指定的页面信息,并返回实体主体。
  • HEAD: 只请求页面的首部。
  • POST: 请求服务器接受所指定的文档作为对所标识的URI的新的从属实体。
  • PUT: 从客户端向服务器传送的数据取代指定的文档的内容。
  • DELETE: 请求服务器删除指定的页面。
  • OPTIONS: 允许客户端查看服务器的性能。

访问baidu,获取一些基本信息:

import requests

response = requests.get("https://www.baidu.com")# 打开网页获取响应
print('response:', type(response))# 打印响应类型,response: 
print('status_code:', response.status_code)# 打印状态码 ,status_code: 200
print('cookie:', response.cookies)#  打印cookie  ,cookie: ]></requestscookiejar[
print(type(response.text)) # 打印字符串形式的json响应体的类型 ,< class 'str'>
print('text:', response.text)   # 打印字符串形式的响应体 ,text: >ç™»å½...•
print('二进制content:', response.content)       # 二进制content, b'\r\n\xe7\x99\xbb\xe5\xbd\x95... \r\n'
print('content:', response.content.decode("utf-8")) # content:  登录...

响应

请求后响应的内容是requests.models.Response对象,需要处理后才能得到我们需要的信息。

requests自动检测编码,可以使用encoding属性查看。

无论响应是文本还是二进制内容,我们都可以用content属性获得bytes对象:

  • encoding:获取当前的编码
  • encoding = 'utf-8' :设置编码
  • headers :以字典对象存储服务器响应头,但是这个字典比较特殊,字典键不区分大小写,若键不存在则返回None
  • requests.headers :返回发送到服务器的头信息
  • cookies :返回cookie
  • history :返回重定向信息,当然可以在请求是加上allow_redirects = false 阻止重定向
  • status_code :响应状态码
  • raw :返回原始响应体,也就是 urllib 的 response 对象,使用 r.raw.read()
  • ok :查看r.ok的布尔值便可以知道是否登陆成功
  • raise_for_status() :失败请求(非200响应)抛出异常
  • text: 得到的是str类型,会自动根据响应头部的字符编码进行解码。
  • content :得到的是bytes类型,需要进行解码Response_get.content.decode(),相当于Response_get.text。字节方式的响应体,会自动为你解码 gzip 和 deflate 压缩。
  • json(): Requests中内置的JSON解码器,以json形式返回,前提返回的内容确保是json格式的,不然解析出错会抛异常

其实使用requset.text避免乱码的方式还有一个,就是发出请求后,获取内容之前使用response.encoding属性来改变编码,例如:

response =requests.get("http://www.baidu.com")
 #设置响应内容的编码方式为utf-8
response.encoding="utf-8"
print(response.text)

二、发送get请求

requests.get(url=url, headers=headers, params=params)

  • url:请求url地址
  • headers:请求头
  • params:查询字符串

1、一个带参数的get请求:

对于带参数的URL,传入一个dict作为params参数,如果值为None的键不会被添加到url中。

import requests
    #将参数写在字典里,通过params传入,params接受字典或序列
    data = {
        "name": "hanson",
        "age": 24
    }
   response = requests.get("http://httpbin.org/get", params=data) #发出一个get请求,获得响应
   print(response.url) #打印url
   print(response.text) #打印响应内容

结果为:

http://httpbin.org/get?name=hanson&age=24
{
   "args": {
     "age": "24", 
     "name": "hanson"
   }, 
   "headers": {
     "Accept": "*/*", 
     "Accept-Encoding": "gzip, deflate", 
     "Host": "httpbin.org", 
     "User-Agent": "python-requests/2.22.0", 
     "X-Amzn-Trace-Id": "Root=1-5e71bb9d-79cfc9e0195befa018426f20"
   }, 
   "origin": "218.106.132.130", 
   "url": "http://httpbin.org/get?name=hanson&age=24"
 }

2、响应json

requests的方便之处还在于,对于特定类型的响应,例如JSON,可以直接获取:

requests里的json方法就是封装了json.loads方法。

import requests
import json
# 发出一个get请求
response = requests.get("http://httpbin.org/get")
# text响应类型
print(type(response.text))
# 直接解析响应json(成字典)
print(response.json())
# 获取响应内容后json进行解析(成字典)
print(json.loads(response.text))
# 直接解析后的相应内容类型
print(type(response.json()))

控制台打印结果:

<class 'str'>
 {'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.18.4'}, 'origin': '124.74.47.82', 'url': 'http://httpbin.org/get'}
 {'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'close', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.18.4'}, 'origin': '124.74.47.82', 'url': 'http://httpbin.org/get'}
< class 'dict'>

3、添加头信息headers

需要传入HTTP Header时,我们传入一个dict作为headers参数:

添加头信息访问:

import requests
# 添加头部信息
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
}
# 发送请求
response = requests.get("https://www.zhihu.com", headers=headers)
# 打印响应
print(response.text)

4、添加和获取cookie信息

equests对Cookie做了特殊处理,使得我们不必解析Cookie就可以轻松获取指定的Cookie:

要在请求中传入Cookie,只需准备一个dict传入cookies参数:

header = {'user-agent': 'my-app/0.0.1''}
cookie = {'key':'value'}
 #发送请求
response = requests.get/post('your url',headers=header,cookies=cookie)
#打印cookie
print(response.cookies)
  
for key, value in response.cookies.items():
   print(key + "=" + value)

三、发送post请求

requests.post(url=url, headers=headers, data=params)

  • url:请求url地址
  • headers:请求头
  • data:发送编码为表单形式的数据

1、一个带参数的Post请求:

要发送POST请求,只需要把get()方法变成post(),然后传入data参数作为POST请求的数据:

import requests
    #参数写在字典里
    data = {
        "name": "hason",
         "age": 23
     }
     #请求时将字典参数赋给data参数
    response = requests.post("http://httpbin.org/post", data=data)
     #打印响应
    print(response.text)

打印结果:

{
   "args": {}, 
   "data": "", 
   "files": {}, 
   "form": {
     "age": "23", 
     "name": "zhaofan"
   }, 
   "headers": {
     "Accept": "*/*", 
     "Accept-Encoding": "gzip, deflate", 
     "Content-Length": "19", 
     "Content-Type": "application/x-www-form-urlencoded", 
     "Host": "httpbin.org", 
     "User-Agent": "python-requests/2.18.4"
   }, 
   "json": null, 
   "origin": "124.74.47.82, 124.74.47.82", 
   "url": "https://httpbin.org/post"
 }

2、传递JSON数据

requests默认使用application/x-www-form-urlencoded对POST数据编码。如果要传递JSON数据,可以直接传入json参数:

params = {'key': 'value'}
r = requests.post(url, json=params) # 内部自动序列化为JSON

3、文件上传

文件上传需要用到请求参数里的files参数:

在读取文件时,注意务必使用'rb'即二进制模式读取,这样获取的bytes长度才是文件的长度。

import requests
# rb,以只读的方式打开二进制文件
files = {"files": open("a.jpg", "rb")}
# 发送post请求携带文件
response = requests.post("http://httpbin.org/post", files=files)
# 响应内容
print(response.text)

响应结果:

{
   "args": {}, 
   "data": "", 
   "files": {
     "files": ""
   }, 
   "form": {}, 
   "headers": {
     "Accept": "*/*", 
     "Accept-Encoding": "gzip, deflate", 
     "Content-Length": "145", 
     "Content-Type": "multipart/form-data; boundary=75c9d62b8f1248a9b6a89741143836b5", 
     "Host": "httpbin.org", 
     "User-Agent": "python-requests/2.18.4"
   }, 
   "json": null, 
   "origin": "124.74.47.82, 124.74.47.82", 
   "url": "https://httpbin.org/post"
 }

request更加方便的是,可以把字符串当作文件进行上传:

import requests
 
url = 'http://127.0.0.1:8080/upload'
files = {'file': ('test.txt', b'Hello Requests.')}     #必需显式的设置文件名
 
r = requests.post(url, files=files)
print(r.text)

四、高级应用

1、session会话维持

会话对象requests.Session能够跨请求地保持某些参数,比如cookies,即在同一个Session实例发出的所有请求都保持同一个cookies,而requests模块每次会自动处理cookies,这样就很方便地处理登录时的cookies问题。

    import requests
    headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
           'Accept-Encoding': 'gzip, deflate, compress',
           'Accept-Language': 'en-us;q=0.5,en;q=0.3',
           'Cache-Control': 'max-age=0',
           'Connection': 'keep-alive',
           'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}
 
  
    #创建session对象
    s = requests.Session()
    s.headers.update(headers)#使用session访问并设置number参数
    s.get("http://httpbin.org/cookies/set/number/123456")
    #session对象再次访问,获取响应内容
    response = s.get("http://httpbin.org/cookies")
    print(response.text)

2、身份验证

auth:认证,接受元祖

基本身份认证(HTTP Basic Auth)

import requests
from requests.auth import HTTPBasicAuth
 
r = requests.get('https://httpbin.org/hidden-basic-auth/user/passwd', auth=HTTPBasicAuth('user', 'passwd'))
print(r.json())

简写:

response = requests.get("http://120.27.34.24:9001/",auth=("user","123"))

另一种非常流行的HTTP身份认证形式是摘要式身份认证,Requests对它的支持也是开箱即可用的:

requests.get(URL, auth=HTTPDigestAuth('user', 'pass')

3、代理设置

proxies = {'http':'ip1','https':'ip2' }
requests.get('url',proxies=proxies)

如果代理需要用户名和密码,则需要这样:

proxies = {
    "http": "http://user:pass@10.10.1.10:3128/",
}

4、证书验证

现在的很多网站都是https的方式访问,所以这个时候就涉及到证书的问题

例如访问12306:

import requests
response = requests.get("https:/www.12306.cn")
print(response.status_code)

会报错,证书错误

解决:加上verify=false(默认是true)

import requests
     #from requests.packages import urllib3
     #urllib3.disable_warnings()
     response = requests.get("https://www.12306.cn", verify=False)
     print(response.status_code)

5、超时时间

timeout,单位:毫秒

r = requests.get('url',timeout=1)           #设置秒数超时,仅对于连接有效

6、重定向与请求历史

使用GET或OPTIONS时,Requests会自动处理位置重定向。

Github将所有的HTTP请求重定向到HTTPS。可以使用响应对象的 history 方法来追踪重定向。 我们来看看Github做了什么:

r = requests.get('http://github.com')
>>> r.url
'https://github.com/'
>>> r.status_code
200
>>> r.history
[]

Response.history 是一个:class:Request 对象的列表,为了完成请求而创建了这些对象。这个对象列表按照从最老到最近的请求进行排序。

如果你使用的是GET或OPTIONS,那么你可以通过 allow_redirects 参数禁用重定向处理:

r = requests.get('http://github.com', allow_redirects=False)
>>> r.status_code
301
>>> r.history
[]

7、其他

  • stream:是否下载获取的内容
  • cert:保存本地SSL证书路径

五、异常处理

所有的异常都是在requests.excepitons中:

示例:

import requests
  
 from requests.exceptions import ReadTimeout,ConnectionError,RequestException
 
 try:
     response = requests.get("http://httpbin.org/get",timout=0.1)
     print(response.status_code)
 except ReadTimeout:
     print("timeout")
 except ConnectionError:
     print("connection Error")
 except RequestException:
     print("error")

测试可以发现,首先被捕捉的异常是timeout超时异常,当把网络断掉就会捕捉到ConnectionError连接异常,如果前面异常都没有捕捉到,最后也可以通过RequestExctption捕捉到。

六、requests库和urllib包对比

1、使用urllib.request

import urllib.parse
import urllib.request
url = "https://api.douban.com/v2/event/list"
params = urllib.parse.urlencode({'loc':'108288','day_type':'weekend','type':'exhibition'})
print(">>>>>>request params is:")
print(params)
 # 发送请求
response = urllib.request.urlopen('?'.join([url, params]))
 # 处理响应
print(">>>>>>Response Headers:")
print(dict(response.info()))
print(">>>>>>Status Code:")
print(response.getcode())
print(">>>>>>Response body:")
print(response.read().decode())

2、使用requests库

import requests
url = "https://api.douban.com/v2/event/list"
params = {'loc':'108288','day_type':'weekend','type':'exhibition'}
print(">>>>>>request params is:")
print(params)

# 发送请求
response = requests.get(url=url,params=params)
# 处理响应
print(">>>>>>Response Headers:")
print(response.headers)
print(">>>>>>Status Code:")
print(response.status_code)
print(">>>>>>Response body:")
print(response.text)

3、区别:

  • 构建参数:在构建请求参数时,第一种需要将请求参数使用urllib库的urlencode方法进行编码预处理,非常麻烦。
  • 请求方法:发送get请求时,第一种使用的urllib库的urlopen方法打开一个url地址,而第二种直接使用requests库的get方法,与http请求方式是对应的,更加直接、易懂。
  • 请求数据:第一种按照url格式去拼接一个url字符串,显然非常麻烦,第二种按顺序将get请求的url和参数写好就可以了。
  • 处理响应:第一种处理消息头部、响应状态码和响应正文时分别使用.info()、.getcode()、.read()方法,第二种使用.headers、.status_code、.text方法,方法名称与功能本身相对应,更方便理解、学习和使用。
  • 连接方式:看一下返回数据的头信息的“connection”,使用urllib库时,“connection”:“close”,说明每次请求结束关掉socket通道,而使用requests库使用了urllib3,多次请求重复使用一个socket,“connection”:“keep-alive”,说明多次请求使用一个连接,消耗更少的资源。
  • 编码方式:requests库的编码方式Accept-Encoding更全。

七、自动登陆"示例:

1、抽屉新热榜

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests


# ############## 方式一 ##############
"""
# ## 1、首先登陆任何页面,获取cookie
i1 = requests.get(url="http://dig.chouti.com/help/service")
i1_cookies = i1.cookies.get_dict()

# ## 2、用户登陆,携带上一次的cookie,后台对cookie中的 gpsd 进行授权
i2 = requests.post(
    url="http://dig.chouti.com/login",
    data={
        'phone': "8615131255089",
        'password': "xxooxxoo",
        'oneMonth': ""
    },
    cookies=i1_cookies
)

# ## 3、点赞(只需要携带已经被授权的gpsd即可)
gpsd = i1_cookies['gpsd']
i3 = requests.post(
    url="http://dig.chouti.com/link/vote?linksId=8589523",
    cookies={'gpsd': gpsd}
)

print(i3.text)
"""


# ############## 方式二 ##############
"""
import requests

session = requests.Session()
i1 = session.get(url="http://dig.chouti.com/help/service")
i2 = session.post(
    url="http://dig.chouti.com/login",
    data={
        'phone': "8615131255089",
        'password': "xxooxxoo",
        'oneMonth': ""
    }
)
i3 = session.post(
    url="http://dig.chouti.com/link/vote?linksId=8589523"
)
print(i3.text)

"""

2、 github

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import requests
from bs4 import BeautifulSoup

# ############## 方式一 ##############
#
# # 1. 访问登陆页面,获取 authenticity_token
# i1 = requests.get('https://github.com/login')
# soup1 = BeautifulSoup(i1.text, features='lxml')
# tag = soup1.find(name='input', attrs={'name': 'authenticity_token'})
# authenticity_token = tag.get('value')
# c1 = i1.cookies.get_dict()
# i1.close()
#
# # 1. 携带authenticity_token和用户名密码等信息,发送用户验证
# form_data = {
# "authenticity_token": authenticity_token,
#     "utf8": "",
#     "commit": "Sign in",
#     "login": "wupeiqi@live.com",
#     'password': 'xxoo'
# }
#
# i2 = requests.post('https://github.com/session', data=form_data, cookies=c1)
# c2 = i2.cookies.get_dict()
# c1.update(c2)
# i3 = requests.get('https://github.com/settings/repositories', cookies=c1)
#
# soup3 = BeautifulSoup(i3.text, features='lxml')
# list_group = soup3.find(name='div', class_='listgroup')
#
# from bs4.element import Tag
#
# for child in list_group.children:
#     if isinstance(child, Tag):
#         project_tag = child.find(name='a', class_='mr-1')
#         size_tag = child.find(name='small')
#         temp = "项目:%s(%s); 项目路径:%s" % (project_tag.get('href'), size_tag.string, project_tag.string, )
#         print(temp)



# ############## 方式二 ##############
# session = requests.Session()
# # 1. 访问登陆页面,获取 authenticity_token
# i1 = session.get('https://github.com/login')
# soup1 = BeautifulSoup(i1.text, features='lxml')
# tag = soup1.find(name='input', attrs={'name': 'authenticity_token'})
# authenticity_token = tag.get('value')
# c1 = i1.cookies.get_dict()
# i1.close()
#
# # 1. 携带authenticity_token和用户名密码等信息,发送用户验证
# form_data = {
#     "authenticity_token": authenticity_token,
#     "utf8": "",
#     "commit": "Sign in",
#     "login": "wupeiqi@live.com",
#     'password': 'xxoo'
# }
#
# i2 = session.post('https://github.com/session', data=form_data)
# c2 = i2.cookies.get_dict()
# c1.update(c2)
# i3 = session.get('https://github.com/settings/repositories')
#
# soup3 = BeautifulSoup(i3.text, features='lxml')
# list_group = soup3.find(name='div', class_='listgroup')
#
# from bs4.element import Tag
#
# for child in list_group.children:
#     if isinstance(child, Tag):
#         project_tag = child.find(name='a', class_='mr-1')
#         size_tag = child.find(name='small')
#         temp = "项目:%s(%s); 项目路径:%s" % (project_tag.get('href'), size_tag.string, project_tag.string, )
#         print(temp)

3、 知乎

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import time

import requests
from bs4 import BeautifulSoup

session = requests.Session()

i1 = session.get(
    url='https://www.zhihu.com/#signin',
    headers={
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
    }
)

soup1 = BeautifulSoup(i1.text, 'lxml')
xsrf_tag = soup1.find(name='input', attrs={'name': '_xsrf'})
xsrf = xsrf_tag.get('value')

current_time = time.time()
i2 = session.get(
    url='https://www.zhihu.com/captcha.gif',
    params={'r': current_time, 'type': 'login'},
    headers={
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
    })

with open('zhihu.gif', 'wb') as f:
    f.write(i2.content)

captcha = input('请打开zhihu.gif文件,查看并输入验证码:')
form_data = {
    "_xsrf": xsrf,
    'password': 'xxooxxoo',
    "captcha": 'captcha',
    'email': '424662508@qq.com'
}
i3 = session.post(
    url='https://www.zhihu.com/login/email',
    data=form_data,
    headers={
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
    }
)

i4 = session.get(
    url='https://www.zhihu.com/settings/profile',
    headers={
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
    }
)

soup4 = BeautifulSoup(i4.text, 'lxml')
tag = soup4.find(id='rename-section')
nick_name = tag.find('span',class_='name').string
print(nick_name)

4、 博客园

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re
import json
import base64

import rsa
import requests


def js_encrypt(text):
    b64der = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCp0wHYbg/NOPO3nzMD3dndwS0MccuMeXCHgVlGOoYyFwLdS24Im2e7YyhB0wrUsyYf0/nhzCzBK8ZC9eCWqd0aHbdgOQT6CuFQBMjbyGYvlVYU2ZP7kG9Ft6YV6oc9ambuO7nPZh+bvXH0zDKfi02prknrScAKC0XhadTHT3Al0QIDAQAB'
    der = base64.standard_b64decode(b64der)

    pk = rsa.PublicKey.load_pkcs1_openssl_der(der)
    v1 = rsa.encrypt(bytes(text, 'utf8'), pk)
    value = base64.encodebytes(v1).replace(b'\n', b'')
    value = value.decode('utf8')

    return value


session = requests.Session()

i1 = session.get('https://passport.cnblogs.com/user/signin')
rep = re.compile("'VerificationToken': '(.*)'")
v = re.search(rep, i1.text)
verification_token = v.group(1)

form_data = {
    'input1': js_encrypt('wptawy'),
    'input2': js_encrypt('asdfasdf'),
    'remember': False
}

i2 = session.post(url='https://passport.cnblogs.com/user/signin',
                  data=json.dumps(form_data),
                  headers={
                      'Content-Type': 'application/json; charset=UTF-8',
                      'X-Requested-With': 'XMLHttpRequest',
                      'VerificationToken': verification_token}
                  )

i3 = session.get(url='https://i.cnblogs.com/EditDiary.aspx')

print(i3.text)

5、 拉勾网

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import requests


# 第一步:访问登陆页,拿到X_Anti_Forge_Token,X_Anti_Forge_Code
# 1、请求url:https://passport.lagou.com/login/login.html
# 2、请求方法:GET
# 3、请求头:
#    User-agent
r1 = requests.get('https://passport.lagou.com/login/login.html',
                 headers={
                     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
                 },
                 )

X_Anti_Forge_Token = re.findall("X_Anti_Forge_Token = '(.*?)'", r1.text, re.S)[0]
X_Anti_Forge_Code = re.findall("X_Anti_Forge_Code = '(.*?)'", r1.text, re.S)[0]
print(X_Anti_Forge_Token, X_Anti_Forge_Code)
# print(r1.cookies.get_dict())
# 第二步:登陆
# 1、请求url:https://passport.lagou.com/login/login.json
# 2、请求方法:POST
# 3、请求头:
#    cookie
#    User-agent
#    Referer:https://passport.lagou.com/login/login.html
#    X-Anit-Forge-Code:53165984
#    X-Anit-Forge-Token:3b6a2f62-80f0-428b-8efb-ef72fc100d78
#    X-Requested-With:XMLHttpRequest
# 4、请求体:
# isValidate:true
# username:15131252215
# password:ab18d270d7126ea65915c50288c22c0d
# request_form_verifyCode:''
# submit:''
r2 = requests.post(
    'https://passport.lagou.com/login/login.json',
    headers={
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
        'Referer': 'https://passport.lagou.com/login/login.html',
        'X-Anit-Forge-Code': X_Anti_Forge_Code,
        'X-Anit-Forge-Token': X_Anti_Forge_Token,
        'X-Requested-With': 'XMLHttpRequest'
    },
    data={
        "isValidate": True,
        'username': '15131255089',
        'password': 'ab18d270d7126ea65915c50288c22c0d',
        'request_form_verifyCode': '',
        'submit': ''
    },
    cookies=r1.cookies.get_dict()
)
print(r2.text)

到此这篇关于Python使用requests库的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

原文出处:https://www.cnblogs.com/springsnow/p/12560454.html

[!--infotagslink--]

相关文章

  • python opencv 画外接矩形框的完整代码

    这篇文章主要介绍了python-opencv-画外接矩形框的实例代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-04
  • PHP 数据库缓存Memcache操作类

    操作类就是把一些常用的一系列的数据库或相关操作写在一个类中,这样调用时我们只要调用类文件,如果要执行相关操作就直接调用类文件中的方法函数就可以实现了,下面整理了...2016-11-25
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • 最炫Python烟花代码全解析

    2022虎年新年即将来临,小编为大家带来了一个利用Python编写的虎年烟花特效,堪称全网最绚烂,文中的示例代码简洁易懂,感兴趣的同学可以动手试一试...2022-02-14
  • python中numpy.empty()函数实例讲解

    在本篇文章里小编给大家分享的是一篇关于python中numpy.empty()函数实例讲解内容,对此有兴趣的朋友们可以学习下。...2021-02-06
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • python-for x in range的用法(注意要点、细节)

    这篇文章主要介绍了python-for x in range的用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-05-10
  • Python 图片转数组,二进制互转操作

    这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • python实现b站直播自动发送弹幕功能

    这篇文章主要介绍了python如何实现b站直播自动发送弹幕,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下...2021-02-20
  • Intellij IDEA连接Navicat数据库的方法

    这篇文章主要介绍了Intellij IDEA连接Navicat数据库的方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借价值,需要的朋友可以参考下...2021-03-25
  • 在数据库里将毫秒转换成date格式的方法

    在开发过程中,我们经常会将日期时间的毫秒数存放到数据库,但是它对应的时间看起来就十分不方便,我们可以使用一些函数将毫秒转换成date格式。 一、 在MySQL中,有内置的函数from_unixtime()来做相应的转换,使用如下: 复制...2014-05-31
  • python Matplotlib基础--如何添加文本和标注

    这篇文章主要介绍了python Matplotlib基础--如何添加文本和标注,帮助大家更好的利用Matplotlib绘制图表,感兴趣的朋友可以了解下...2021-01-26
  • 解决python 使用openpyxl读写大文件的坑

    这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
  • C#操作本地文件及保存文件到数据库的基本方法总结

    C#使用System.IO中的文件操作方法在Windows系统中处理本地文件相当顺手,这里我们还总结了在Oracle中保存文件的方法,嗯,接下来就来看看整理的C#操作本地文件及保存文件到数据库的基本方法总结...2020-06-25
  • 如何解决局域网内mysql数据库连接慢

    通过内网连另外一台机器的mysql服务, 确发现速度N慢! 等了大约几十秒才等到提示输入密码。 但是ping mysql所在服务器却很快! 想到很久之前有过类似的经验, telnet等一些服务在连接请求的时候,会做一些反向域名解析(如果...2015-10-21
  • python 计算方位角实例(根据两点的坐标计算)

    今天小编就为大家分享一篇python 计算方位角实例(根据两点的坐标计算),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-27
  • MySQL快速复制数据库数据表的方法

    某些时候,例如为了搭建一个测试环境,或者克隆一个网站,需要复制一个已存在的mysql数据库。使用以下方法,可以非常简单地实现。假设已经存在的数据库名字叫db1,想要复制一份,命名为newdb。步骤如下:1. 首先创建新的数据库newd...2015-10-21
  • python实现双色球随机选号

    这篇文章主要为大家详细介绍了python实现双色球随机选号,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-05-02