字符串解码和查看请求信息
# lebo='天津杰森'
# print(lebo)
# 字符串编码
# lebo_text=lebo.encode()
#print(lebo_text)
# 字符串解码
# print(lebo_text.decode())
import requests
url='http://www.baidu.com'
# 发起get请求
response=requests.get(url)
# 查看状态码
# print(response.status_code)
# 响应头
# print(response.headers)
# 请求头
print(response.request.headers)
# 指定utf-8格式,进行解码
# response.encoding='utf-8'
# print(response.text)
# 默认使用utf-8进行解码
# print(response.content.decode())
发送get请求
import requests
# url='https://www.baidu.com'
url='https://www.zhihu.com'
#构建请求头
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
}
response=requests.get(url,headers=headers)
#打印response对应的request请求头
print(response.request.headers)
#打印响应状态吗
print(response.status_code)
# print(response.content.decode())
pip list打开编辑器所有已安装的包
![](https://img-blog.csdnimg.cn/20190717182945333.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI3MjUxNDc1,size_16,color_FFFFFF,t_70)
requests模块文档:http://cn.python-requests.org/zh_CN/latest/
![](https://img-blog.csdnimg.cn/2019071718523459.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI3MjUxNDc1,size_16,color_FFFFFF,t_70)
保存网页到本地
import requests
url='https://www.baidu.com/s'
#构建请求头
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
}
params={
'wd':'python'
}
response=requests.get(url,headers=headers,params=params)
with open('test.html','w',encoding='utf-8') as f:
f.write(response.content.decode())
发送请求
import requests
class login(object):
def __init__(self):
self.url=' http://106.13.46.164:8080/iwebshop/index.php?controller=simple&action=login_act'
self.data={
'login_info': 'zhang',
'password': '111111'
}
self.headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
}
def post_method(self):
response=requests.post(url=self.url,headers=self.headers,data=self.data)
return response.content
if __name__=='__main__':
login=login()
res=login.post_method()
with open('test.html', 'w', encoding='utf-8') as f:
f.write(res.decode())
# 断言是否登录成功
assert 'zhang' in res.decode()
jason接口
import requests
import json
url='http://106.13.46.164:8080/iwebshop/index.php?controller=simple&action=joinCart&goods_id=147&random=0.771087505969752&type=goods'
#构建请求头
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
}
response=requests.get(url,headers=headers)
print(response.content.decode('unicode-escape'))
# jason 格式转换为字典
dict_data=json.loads(response.content)
print(dict_data)
# 断言 false 小写传过来变为大写
assert dict_data['isError'] is False
# with open('test.html','w',encoding='utf-8') as f:
# f.write(response.content.decode())
设置代理
import requests
class login(object):
def __init__(self):
self.url=' http://106.13.46.164:8080/iwebshop/index.php?controller=simple&action=login_act'
self.data={
'login_info': 'zhang',
'password': '111111'
}
self.headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
}
self.proxies = {
'http': 'http://127.0.0.1:8888'
}
def post_method(self):
response=requests.post(url=self.url,headers=self.headers,data=self.data,proxies=self.proxies)
return response.content
if __name__=='__main__':
login=login()
res=login.post_method()
# with open('test.html', 'w', encoding='utf-8') as f:
# f.write(res.decode())
# assert 'zhang' in res.decode()
selenium结合requests 后边在进行整理 切记