标签:github 定向 latest 跨域 list 它的 tps oss 完整
import requests
r = requests.get('https://api.github.com/events')
r = requests.post('http://httpbin.org/post', data = {'key':'value'})
url = 'http://httpbin.org/get'
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get(url=url, params=payload)
print(r.url)
# http://httpbin.org/get?key2=value2&key1=value1
url = 'http://httpbin.org/get'
payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
r = requests.get(url=url, params=payload)
print(r.url)
# http://httpbin.org/get?key1=value1&key2=value2&key2=value3
import requests
r = requests.get('https://api.github.com/events')
print(r.text) # 获取响应内容
print(r.encoding) # 'utf-8'
import requests
url = 'https://b-ssl.duitang.com/uploads/item/201504/29/20150429075539_cy8vV.jpeg'
r = requests.get(url)
path = 'img/1.jpg'
with open(path, 'wb') as f:
"""写入一张照片"""
f.write(r.content)
JSON 解码器
import requests
r = requests.get('https://api.github.com/events')
print(r.json())
print(r.text)
r = requests.get('http://httpbin.org/get')
print(r.status_code) #200
print(r.status_code == requests.codes.ok) # True
r = requests.get('http://httpbin.org/get')
print(r.headers)
print(r.headers['Content-Type'])
url = 'https://api.github.com/some/endpoint'
headers = {'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers=headers)
url = "http://httpbin.org/post"
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post(url, data=payload)
print(r.text)
你还可以为 data 参数传入一个元组列表。在表单中多个元素使用同一 key 的时候,这种方式尤其有效:
url = "http://httpbin.org/post"
payload = (('key1', 'value1'), ('key1', 'value2'))
r = requests.post(url, data=payload)
print(r.text)
import json
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
r = requests.post(url, data=json.dumps(payload))
r = requests.post(url, json=payload)
print(r.text)
url = 'http://httpbin.org/post'
files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=files)
print(r.text)
url = 'http://httpbin.org/post'
files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
r = requests.post(url, files=files)
print(r.text)
url = 'http://httpbin.org/post'
files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}
r = requests.post(url, files=files)
print(r.text)
url = 'http://example.com/some/cookie/setting/url'
r = requests.get(url)
data = r.cookies['example_cookie_name']
print(data)
print(r.cookies['example_cookie_name'])
url = 'http://httpbin.org/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)
print(r.text)
Cookie 的返回对象为 RequestsCookieJar,它的行为和字典类似,但接口更为完整,适合跨域名跨路径使用。你还可以把 Cookie Jar 传到 Requests 中:
jar = requests.cookies.RequestsCookieJar()
jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere')
url = 'http://httpbin.org/cookies'
r = requests.get(url, cookies=jar)
print(r.text)
r = requests.get('https://github.com', timeout=2)
print(r.text)
Github 将所有的 HTTP 请求重定向到 HTTPS:
r = requests.get('http://github.com')
print(r.url)
print(r.status_code)
print(r.history)
参考:Requests快速上手
标签:github 定向 latest 跨域 list 它的 tps oss 完整
原文地址:https://www.cnblogs.com/xuwei1/p/11069483.html