HTTP通訊協定是一種伺服器和客戶端之間傳送資料的通訊協定,可以向Web伺服器請求所需的HTML網頁。詳細的請求過程如下:
import requests
r = requests.get("http://www.google.com")
print(r.status_code)
if r.status_code == 200:
print("請求成功...")
else:
print("請求失敗...")
import requests
post_data = {'name': '陳柔安', 'score': 95}
r = requests.post("http://httpbin.org/post", data=post_data)
print(r.text)
import requests
r = requests.get("https://rouan0903.github.io/")
print(r.text)
print(r.encoding)
import requests
r = requests.get("http://www.google.com/404")
print(r.status_code)
print(r.status_code == requests.codes.ok)
print(r.raise_for_status())