#发get请求一种是有参数的,一种是没参数的(params=par)
#发post请求(一种是有body(json=body,data=body),一种是没有body(params=par))
#返回的内容raw就是text,json就是json
‘’’
什么情况下要传头部参数,什么情况下不要传头部参数(fiddler先调试,看看去掉头部能不能请求成功,再看带上头部能不能请求成功。然后再看头部哪些参数必要的)
cookies是动态的怎么办
‘’’
#字符串取值要用正则,可以把字符串转换成字典,然后再去取
‘’’
import requests
url = ‘http://admin.’
body = {
“account”: “admin”,
“password”: “123456”
}
r = requests.post(url, data=body)#在body里就传data,在url的问号之后就传parmars
print(r.json())
print(r.headers[‘Content-Type’])
print(r.status_code)
print(type(r.json()))
import requests
url = “https://www.baidu.com/”
r = requests.get(url)
print(r.text)
print(r.content.decode(‘utf-8’)) #html 乱码
print(type(r.text))
print(r.url)
print(r.encoding)
print(r.cookies)
cook = dict(r.cookies)#字符串转字典格式
print(cook[‘BDORZ’])
print(r.headers)
a = 2
b = 3
if a == b:
print(‘ok’)
else:
raise NameError
assert a == b
‘’’