01网络请求-requests库

安装和文档地址

直接使用pip可以非常方便的安装
pip install requests
中文文档:http://docs.python-requests.org/zh_CN/latest/index.html
github地址:https://github.com/requests/requests

发送get请求

  1. 最简单的发送get请求
import requests
response = requests.get('https://www.baidu.com/')
  1. respnse.text和response.content的区别
    • response.content:这个是直接从网络上抓取的数据,没有经过任何的解码,所以是bytes类型,其实在硬盘上和网络上的字符串都是bytes类型
    • response.text:这个是requests,将response.content进行解码的字符串,解码需要指定一个编码方式,requests对根据自己的猜测来判断解码的方式,所以有时候会产生乱码,这个时候就应该使用response.content.decode('utf-8')来进行手动解码
import requests

response = requests.get('https://www.baidu.com/')
# 这个返回的是Unicode格式的数据类型,也就是字符串,
# 但是使用这个有弊端,中文乱码,还不能自己指定打印出来的字符类型
print(type(response.text))
print(response.text)
# 这个返回的是字节流数据,也就是bytes类型,使用这个打印出来的
# 中文依然是乱码,但是这个可以进行解码,decode('utf-8')
print(type(response.content))
print(response.content.decode('utf-8'))
# 这是一些常用的方法
print(response.url) # 查看完整的url地址
print(response.encoding) # 查看响应头部的字符编码
print(response.status_code) # 查看响应码
  • 《01网络请求-requests库》 text打印出来的.png
  • [图片上传失败…(image-2cf12e-1523456588525)]
  1. 添加headers和查询参数
    • 如果想添加 headers,可以传入headers参数来增加请求头中的headers信息
    • 如果要将参数放在url中传递,可以利用 params 参数
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()
resp = requests.get('http://www.baidu.com/s',params=kw,headers=headers)

# 这里写入的时候需要注意编码格式
with open('baidu.html','w',encoding='utf-8') as fp:
    # 写入的格式一般就是这样
    fp.write(resp.content.decode('utf-8'))
print(resp.url)

《01网络请求-requests库》 看到打印的url.png

发送post请求

  1. 最基本的POST请求可以使用post方法
response = requests.post("http://www.baidu.com/",data=data)
  1. 传入data数据
    • 这时候就不要再使用urlencode进行编码了,直接传入一个字典进去就可以了
#encoding: utf-8
import requests

url = 'https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false&isSchoolJob=0'

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
    'Referer':'https://www.lagou.com/jobs/list_python?city=%E5%85%A8%E5%9B%BD&cl=false&fromSearch=true&labelWords=&suginput='
}

data = {
    'first':'true',
    'pn':3,
    'kd':'python'
}

resp = requests.post(url=url,data=data,headers=headers)
# 如果是json数据,直接可以调用json方法,返回的字典
print(resp.json())

《01网络请求-requests库》 获取到返回的json数据.png

使用代理

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

#encoding: utf-8
import requests
url = 'https://httpbin.org/ip'
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
}

proxy = {
    'http':'117.28.144.33:21879'
}

resp = requests.get(url,headers=headers,proxies=proxy)
print(resp.text)

cookie

如果在一个响应中包含了cookie,那么可以利用cookies属性拿到这个返回的cookie

#encdoing: utf-8
import requests

resp = requests.get('https://www.baidu.com')
# 获取cookie信息
# print(resp.cookies)
# 以字典的形式获取cookie信息
print(resp.cookies.get_dict())

session

之前使用urllib库,是可以使用opener发送多个请求,多个请求之间是可以共享cookie的,
如果使用requests也要达到共享cookie的目的,那么这个时候就可以使用session对象

#encdoing: utf-8
import requests

url = 'http://www.renren.com/PLogin.do'
data = {'email':'15837503603','password':'python'}

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

# 登陆,这个session中已经存了cookie信息,以后用session访问
session = requests.session()
session.post(url=url,headers=headers,data=data)

# 访问你想访问的个人主页
url2 = 'http://www.renren.com/880151247/profile'
resp = session.get(url=url2,headers=headers)
print(resp.content.decode('utf-8'))

处理不信任的SSL证书

对于那些已经被信任的SSL整数的网站,比如https://www.baidu.com/,那么使用requests直接就可以正常的返回响应

resp = requests.get('http://www.12306.cn/mormhweb/',verify=False)
print(resp.content.decode('utf-8'))
    原文作者:山丘i
    原文地址: https://www.jianshu.com/p/6ac050d84db1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞