test_01.py
import requests
r = requests.get('https://www.baidu.com/')
print(type(r)) # <class 'requests.models.Response'>
print(r.status_code) # 200
print(type(r.text)) # <class 'str'>
print(r.text)
print(r.cookies) # <RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
# 其余请求类型的使用
r = requests.post('http://httpbin.org/post')
r = requests.put('http://httpbin.org/put')
r = requests.delete('http://httpbin.org/delete')
r = requests.head('http://httpbin.org/get')
# print(r.headers)
r = requests.options('http://httpbin.org/get')
test_02.py
import requests
r = requests.get('http://httpbin.org/get')
print(r.text)
r"""
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.24.0",
"X-Amzn-Trace-Id": "Root=1-6103b24e-7336e8ca7bbecdad06161609"
},
"origin": "124.160.154.45",
"url": "http://httpbin.org/get"
}
"""
test_03.py
import requests
data = {
'name': 'germey',
'age': 22
}
# r = requests.get("http://httpbin.org/get", params=data)
r = requests.get('http://httpbin.org/get?name=germey&age=22')
print(r.text)
r"""
{
"args": {
"age": "22",
"name": "germey"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.24.0",
"X-Amzn-Trace-Id": "Root=1-6103b3b8-65814dc91fc64c8403bce346"
},
"origin": "124.160.154.45",
"url": "http://httpbin.org/get?name=germey&age=22"
}
"""
r"""
{
"args": {
"age": "22",
"name": "germey"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.24.0",
"X-Amzn-Trace-Id": "Root=1-6103b3d9-3b0b54527368d3255b4f1174"
},
"origin": "124.160.154.45",
"url": "http://httpbin.org/get?name=germey&age=22"
}
"""
test_04.py
import requests
r = requests.get("http://httpbin.org/get")
print(type(r.text)) # <class 'str'>
print(r.json())
print(type(r.json())) # <class 'dict'>
test_05.py
import requests
import re
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
}
r = requests.get("https://www.zhihu.com/explore", headers=headers)
pattern = re.compile('explore-feed.*?question_link.*?>(.*?)</a>', re.S)
titles = re.findall(pattern, r.text)
print(titles)
test_06.py
import requests
r = requests.get("https://github.com/favicon.ico")
print(r.text)
print(r.content)
test_07.py
import requests
r = requests.get("https://github.com/favicon.ico")
with open('favicon.ico', 'wb') as f:
f.write(r.content)
test_08.py
import requests
r = requests.get("https://www.zhihu.com/explore")
print(r.text)
print('--'*10)
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'}
r = requests.get("https://www.zhihu.com/explore", headers=headers)
print(r.text)
test_09.py
import requests
data = {'name': 'germey', 'age': '22'}
r = requests.post("http://httpbin.org/post", data=data)
print(r.text)
# print(r.content)
test_10.py
import requests
r = requests.get('http://www.jianshu.com')
print(type(r.status_code), r.status_code)
print(type(r.headers), r.headers)
print(type(r.cookies), r.cookies)
print(type(r.url), r.url)
print(type(r.history), r.history)