import requests
response = requests.get('http://httpbin.org/get')
print(response.text)
import requests
response = requests.get('http://httpbin.org/get?name=0bug&age=25')
print(response.text)
import requests
data = {
'name': '0bug',
'age': 25
}
response = requests.get('http://httpbin.org/get', params=data)
print(response.text)```
#### 3.
### 二、POST请求
```python
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36'
}
response = requests.get('https://www.baidu.com/', headers=headers)
print(response.status_code)
import requests
response = requests.get('http://httpbin.org/get')
print(type(response.text))
print(response.json())
print(type(response.json()))
import requests
response = requests.get('https://github.com/favicon.ico')
print(type(response.text))
print(type(response.content))
print(response.text)
print(response.content)
import requests
response = requests.get('https://github.com/favicon.ico')
print(type(response.text))
print(type(response.content))
print(response.text)
print(response.content)
import requests
response = requests.get('https://github.com/favicon.ico')
with open('img.ico','wb') as f:
f.write(response.content)
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36'
}
response = requests.get('https://www.baidu.com/', headers=headers)
print(response.status_code)
import requests
data = {'name':'0bug'}
response = requests.post('http://httpbin.org/post',data=data)
print(response.text)
import requests
data = {'name': '0bug'}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36'
}
response = requests.post('http://httpbin.org/post', data=data, headers=headers)
print(response.json())
import requests
response = requests.get('http://www.jianshu.com')
print(type(response.status_code), response.status_code)
print(type(response.headers), response.headers)
print(type(response.cookies), response.cookies)
print(type(response.url), response.url)
print(type(response.history), response.history)
import requests
response = requests.get('http://www.jianshu.com')
print(type(response.status_code), response.status_code)
print(type(response.headers), response.headers)
print(type(response.cookies), response.cookies)
print(type(response.url), response.url)
print(type(response.history), response.history)
import requests
response = requests.get('https://www.baidu.com')
print(response.cookies)
for key,value in response.cookies.items():
print(key+'='+value)
import requests
response = requests.get('https://www.baidu.com')
print(response.cookies)
for key,value in response.cookies.items():
print(key+'='+value)
import requests
s = requests.Session()
s.get('http://httpbin.org/cookies/set/number/123456')
response = s.get('http://httpbin.org/cookies')
print(response.text)
import requests
response = requests.get('https://www.12306.cn')
print(response.status_code)
import requests
response = requests.get('https://www.12306.cn',verify=False)
print(response.status_code)
import requests
import urllib3
urllib3.disable_warnings()
response = requests.get('https://www.12306.cn',verify=False)
print(response.status_code)
import requests
response = requests.get('https://www.12306.cn',cert=('/path/server.crt','/path/key'))
print(response.status_code)
import requests
proxies = {
'http': 'http://127.0.0.1:9743',
'https': 'https://127.0.0.1:9743'
}
response = requests.get('https://www.taobao.com', proxies=proxies)
print(response.status_code)
import requests
proxies = {
'http': 'http://127.0.0.1:9743',
'https': 'https://127.0.0.1:9743'
}
response = requests.get('https://www.taobao.com', proxies=proxies)
print(response.status_code)
pip install requests[socks]
import requests
proxies = {
'http': 'socks5://127.0.0.1:9742',
'https': 'socks5://127.0.0.1:9742',
}
response = requests.get('https://www.taobao.com', proxies=proxies)
print(response.status_code)
import requests
response = requests.get('https://www.taobao.com',timeout=0.01)
print(response.status_code)
import requests
from requests.exceptions import ReadTimeout
try:
response = requests.get('https://www.taobao.com', timeout=0.01)
print(response.status_code)
except ReadTimeout:
print('time out')
import requests
from requests.auth import HTTPBasicAuth
r = requests.get('http://127.0.0.1:8080', auth=HTTPBasicAuth('user', '123'))
print(r.status_code)
import requests
r = requests.get('http://127.0.0.1:8080', auth=('user', '123'))
print(r.status_code)
import requests
from requests.exceptions import ReadTimeout, HTTPError, RequestException
try:
response = requests.get('http://httpbin.org/get', timeout=0.01)
print(response.status_code)
except ReadTimeout:
print('Timeout')
except HTTPError:
print('HTTP REEOR')
except RequestException:
print('Error')
借鉴地址:https://www.cnblogs.com/yunlongaimeng/p/9802119.html