python--requests库

请求库

虽然Python的标准库中urllib模块已经包含了平常我们使用的大多数功能,但是它的API使用起来让人感觉不太好,而请求库的宣传是“HTTP for Humans”,说明使用更加简洁方便。

安装和文档地址:

利用pip可以非常网求方便的安装:

pip install requests

官方文档 

中文文档

github上

发送GET请求:

  1. 简单最发送的get请求就是通过requests.get来调用:

    response = requests.get("http://www.baidu.com/")
    
  2. 添加头和查询参数:
    如果想添加头,可以传入头信息。如果要将参数放在

     import requests
    
     kw = {'wd':'中国'}
    
     headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
    
     # params 接收一个字典或者字符串的查询参数,字典类型自动转换为url编码,不需要urlencode()
     response = requests.get("http://www.baidu.com/s", params = kw, headers = headers)
    
     # 查看响应内容,response.text 返回的是Unicode格式的数据
     print(response.text)
    
     # 查看响应内容,response.content返回的字节流数据
     print(response.content)
    
     # 查看完整url地址
     print(response.url)
    
     # 查看响应头部字符编码
     print(response.encoding)
    
     # 查看响应码
     print(response.status_code)
    

发送POST请求:

  1. 最基本的POST可以请求使用post方法:

    response = requests.post("http://www.baidu.com/",data=data)
    
  2. 传入数据数据:
    这时候就不要再使用urlencode。进行编码了,直接传入一个字典进去就可以了比如请求拉勾网的数据的代码

     import requests
    
     url = "https://www.lagou.com/jobs/positionAjax.json?city=%E6%B7%B1%E5%9C%B3&needAddtionalResult=false&isSchoolJob=0"
    
     headers = {
         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
         'Referer': 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput='
     }
    
     data = {
         'first': 'true',
         'pn': 1,
         'kd': 'python'
     }
    
     resp = requests.post(url,headers=headers,data=data)
     # 如果是json数据,直接可以调用json方法
     print(resp.json())
    

使用代理:

使用requests添加代理也。非常简单,只要在请求的方法中(比如get或者post)传递proxies。参数就可以了示例代码如下:

import requests

url = "http://httpbin.org/get"

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
}

proxy = {
    'http': '171.14.209.180:27829'
}

resp = requests.get(url,headers=headers,proxies=proxy)
with open('xx.html','w',encoding='utf-8') as fp:
    fp.write(resp.text)

曲奇饼:

如果在一个响应中包含了cookie,可以那么利用cookies属性拿到这个报道查看的cookie值:

import requests

url = "http://www.renren.com/PLogin.do"
data = {"email":"[email protected]",'password':"pythonspider"}
resp = requests.get('http://www.baidu.com/')
print(resp.cookies)
print(resp.cookies.get_dict())

会议:

使用之前urllib库,可以的英文使用opener发送多个请求,请求多个之间的英文可以共享cookie的。那么如果使用requests,达到也要共享cookie的目的,可以那么使用requests库给我们提供的session对象。注意,的这里session不是web开发中的那个会议上,这个地方只是一个会话的对象而已以登录人人网为例,使用。requests来实现示例代码如下:

import requests

url = "http://www.renren.com/PLogin.do"
data = {"email":"",'password':""}  # 自己的账号密码
headers = {
    'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
}

# 登录
session = requests.session()
session.post(url,data=data,headers=headers)

# 访问大鹏个人中心
resp = session.get('http://www.renren.com/880151247/profile')

print(resp.text)

不被信任的SSL证书:

对于那些已经不被信任的SSL证书的网站,可以选择忽视它,进而直接访问

resp = requests.get('http://www.12306.cn/mormhweb/',verify=False)
print(resp.content.decode('utf-8'))

猜你喜欢

转载自blog.csdn.net/qq_40727267/article/details/85240224