requests库
源码分析
pip install requests
练习
两种,第二种是装饰器
class Person:
def __init__(self,name):
self._name = name
def name(self):
return self._name
p = Person('葫芦娃')
print(p.name())
class Person:
def __init__(self,name):
self._name = name
@property
def name(self):
return self._name
p = Person('葫芦娃')
print(p.name)
模拟请求过程
模拟requests请求过程
import requests
r = requests.get('https://www.woyaogexing.com/touxiang/weixin/2020/937715.html')
# r.encoding = 'utf8'
# print(r)
# print(type(r))
# print(r.status_code)
# print(r.encoding)
# print(r.headers['content-type'])
# print(r.text)
html = r.text
with open('h.html','w',encoding='utf8') as f:
f.write(html)
url = 'http://apis.juhe.cn/mobile/get?'
app_key = '申请的ApiKey'
# 要传递的参数
params = {
'phone' : '1580133'
'key': app_key
}
response = requests.get(url,params=params).json
print(response)
响应内容
响应图片内容
r = requests.get('https://img2.woyaogexing.com/2020/02/25/e95692a98d77416d9fa5ebe697456c6f!400x400.jpeg')
# print(r.headers['content-type'])
# print(r.content)
with open('test.jpeg','wb') as f:
f.write(r.content)
响应api接口-ip地址查询
url = 'http://apidata.chinaz.com/CallAPI/ip?'
app_key = '这是我们要申请的ApiKey'
# 要传递的参数
params = {
'ip' : '114.100.254.67',
'key': app_key
}
response = requests.get(url,params=params)
print(response.headers['content-type'])
print(response.json())
定制头部信息
模拟
# 模拟
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36',
'xm-sign': '3985ddedad15984c5a2a66ebf0cf36a0(39)1582618341558(13)1582618401259'
}
url = 'https://www.ximalaya.com/revision/play/v1/audio?id=231012348&ptype=1'
r = requests.get(url,headers=headers)
ret = r.text
print(ret)
响应头部信息
# 响应头部信息
url = 'https://www.woyaogexing.com/touxiang/weixin/2020/937715.html'
r = requests.get(url)
if r.status_code == 200:
# print(r.text)
h = r.headers
print(h)
cookie和session
# cookie 和 session
url = 'https://www.woyaogexing.com/touxiang/weixin/2020/937715.html'
r = requests.get(url)
# print(r.cookies)
username = 'admin'
pwd = '123'
session = requests.session()
session.auth = (username,pwd)
print(session.auth)