requests库的基本使用

1.发送get请求

import requests

# response=requests.get('http://www.baidu.com')
# 查看响应内容,返回的是已经解码的内容
# response.text  服务器返回的数据,已解码。解码类型:根据HTTP头部对响应的编码做出有根据的推测,推测的文本编码
# print(type(response.text))
# print(response.text)
# 百度返回的text有乱码,说明解码猜测的编码方式不对

# 查看响应内容
# print(type(response.content))
# print(response.content.decode('utf-8'))
# 解码正确,没有乱码

# 查看完整url地址
# print(response.url)

# 查看响应头部字符编码
# print(response.encoding)

# 查看响应码
# print(response.status_code)


params = {'wd': '中国'}
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
#params传入,会自动进行编码
response=requests.get('http://www.baidu.com/s',headers=headers,params=params)
print(response.url)
with open('baidu.html','w',encoding='utf-8') as f:
    f.write(response.content.decode('utf-8'))
View Code

2.发送post请求

import requests

data = {
    'first': True, 'pn': 1, 'kd': 'python'
}
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36',
    'Referer': 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=',
    'Cookie':'JSESSIONID=ABAAABAAAFCAAEGE19E4DE9949656042D040782B344E314; SEARCH_ID=94069a753d8a4157a4b8a44284d4b719; user_trace_token=20190404002147-ba37c7c8-aa84-4a31-8171-738e6bcfadf2; X_HTTP_TOKEN=42daf4b72327b2817058034551bf5e71415983ed09'
}

response = requests.post('https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false',headers=headers,data=data)
print(response.text)
#把传来的数据转成原本数据类型(如果传输的是json格式的字符串)
# print(response.json())

#测试
# ret=response.text
# # import json
# # ret=json.loads(ret)
# # print(type(ret))
View Code

3.使用代理

import requests

#不使用代理
# response=requests.get('http://httpbin.org/ip')
# print(response.text)

#使用代理
#尽量使用高匿名的代理,透明的话,它依然能识别原来的ip地址。
proxy={'http':'112.85.149.79:9999'}
response=requests.get('http://httpbin.org/ip',proxies=proxy)
print(response.text)
View Code

4.处理cookie信息

import requests

# response=requests.get('http://www.baidu.com')
#返回的是一个对象
# print(response.cookies)
#获取字典形式信息
# print(response.cookies.get_dict())


#session
#之前使用的urlib库,是可以使用opener发送多个请求,多个请求之间是可以共享cookie的。那么如果使用requests,
#也要达到共享cookie的目的,那么可以使用requests库提供的session对象。它简化了我们每次模拟请求时都要带上cookie
#的复杂操作,使用session它自己会帮我们带上headers里面的cookie信息
url='http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2019341135380'
session=requests.session()
data={
    'email':'9@qq.com',
    'password':'pythonspr'
}
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
session.post(url,data=data,headers=headers)
#只有登录后才能查看大鹏的页面
response=session.get('http://www.renren.com/880151247/profile',headers=headers)
with open('renren.html','w',encoding='utf-8') as f:
    f.write(response.text)
#查看页面,确实登录成功
View Code

5.处理不信任的ssl证书

#处理不信任的ssl证书,加上verify=False就可以了
import requests
resp=requests.get('http://www.12306.cn',verify=False)
print(resp.text)
View Code

 

posted @ 2019-04-29 02:23  徐大  阅读(214)  评论(0编辑  收藏  举报