# coding=utf-8
__author__ = 'Jeff.xie'
import requests
import json
d="""
"usrname":"张三","pwd":"123456789"
"""
url='https://www.baidu.com'
# r=requests.get(url,data=d.encode('utf8'))
# print(r.status_code)
# print(r.text.encode('utf8'))
# request的常用参数如下所示
# def request(self, method, url,
# params=None, data=None, headers=None, cookies=None, files=None,
# auth=None, timeout=None, allow_redirects=True, proxies=None,
# hooks=None, stream=None, verify=None, cert=None, json=None):
"""
import bs4
r2=requests.get(url,json='{"usrname":"张三","pwd":"123456789"}')
print(r2.status_code)
a=r2.apparent_encoding
print(a)
r2.encoding = '2312' #编码格式,解决中文乱码
print("KK")
a=r2.apparent_encoding #获取文本默认编码
print(a)
if r2.status_code == 200:
with open('D:\\test.html','w',encoding='utf-8') as fp:
fp.write(r2.text)
# soup = bs4.BeautifulSoup(r2.text, 'D:\\test2.html')
"""
"""
proxies={ #设置代理,可以fiddler抓包
'http':'http:127.0.0.1:8888',
'https':'http:127.0.0.1:8888'
}
params={'wd':'iphone',
'rsv_spt':'1'}
# response = requests.get("http://www.baidu.com/s",params=params,proxies=proxies) #需要打开fiddler,设置代理
response = requests.get("http://www.baidu.com/s",params=params)
print(response.text)
"""
"""
response = requests.get("http://mirrors.sohu.com")
print(response.text)
"""
"""
#构建请求头
headers={
'user-agent':'my-app/0.0.1',
'auth-type':'jwt-token'}
# response = requests.post("http://httpbin.org/post",headers=headers)
response = requests.get("http://www.baidu.com/s",headers=headers)
response.encoding = 'utf8'
print(response.text)
# print(response.text.decode('utf8'))
"""
# payload="""
#
#
# 良好
#
# 暂无
#
# """
# response = requests.post("http://httpbin.org/post",data=payload.encode('utf8')) #含有中文字符的data需要设置编码
# print(response.text)
"""
payload={'key1':'v1',
'key2':'v2'}
response = requests.post("http://httpbin.org/post",data=payload) #dict没有encode方法
response = requests.post("http://httpbin.org/post",data="key1=v1&key2=v2".encode('utf8'))#字符串可以使用encode
print(response.text)
"""
"""
payload={'Overall':'良好',
'Progress':'30%',
'Problems':[
{
'No':1,
'desc':'问题1'
},
{
'No':1,
'desc':'问题2'
}
]
}
response = requests.post("http://httpbin.org/post",data=json.dumps(payload,ensure_ascii=False).encode()) #字典类型转换成json格式字符串,
# response = requests.post("http://httpbin.org/post",json=json.payload) #和上面的效果是一样的
print(response.text)
"""
"""
response = requests.get("http://mirrors.sohu.com")
print(response.status_code) #获取状态码
print(response.text) #原始数据为字节串
print(response.encoding) #utf8
print(dict(response.headers)) #转换成字典格式
print(dict(response.headers)['Content-Type'])#text/html; charset=utf8
print(response.headers['Content-Type']) #text/html; charset=utf8 #可以直接安装dict格式提取参数
#request会根据响应消息头(Content-Type)对编码格式做推测,应为这个Content-Type是utf8,所以response.encoding是utf8
response.encoding='utf8' #自定义设置编码
response = requests.get("http://www.python3.vip")
response.encoding='utf8'
print(response.encoding)
print(response.text)
response = requests.get("http://mirrors.sohu.com")
print(response.content)#获取字节串
print(response.content.decode('utf8'))#自己解码
"""
"""
response = requests.post("http://httpbin.org/post",data={1:1,2:2})
text=response.content.decode('utf8')
obj = json.loads(text) #转换成字典格式
print(obj)
print(obj['form'])
print(obj['headers'])
print(obj['headers']['Content-Type'])
"""
response = requests.get("http://pay.sohu.com/")
print(response.content)#获取字节串
print(response.content.decode('utf8'))#自己解码
def printResponse(response):
print("\n\n------------HTTP response *begin--------------")
print(response.status_code)
for k, v in response.headers.items():
print('%s: %s' %(k,v))
print(response.cookies.get_dict()) #获取cookie字典
print(response.content.decode('utf8'))
print("----------------HTTP response *end----------------")
#使用Session可以把response中返回的cookie自动存放到session,下一个请求会自动携带cookie去对服务器进行请求,不需要对cookie赋值
session = requests.Session()
# response = session.post("http://httpbin.org/post",data={1:1,2:2})
response = session.get("https://mp.csdn.net/?spm=1001.2101.3001.4503")
printResponse(response)
response.encoding = response.apparent_encoding
print(response.apparent_encoding) #使用apparent_encoding可获取程序真实编码 ascii