requests文件_json:php
#coding:utf-8 import requests import json #url="http://httpbin.org/post" #data textfile newurl="http://pythonscraping.com/pages/processing2.php" #任意文件。-文件名根據php接口 data={"hello":"world"} textfile={"file":open("test.txt","r")} zipfile={"uploadFile":open("1.rar","rb")} #r1=requests.post(url,data=data) #print r1.text print("--------------------------------------") r2=requests.post(newurl,files=zipfile) print(r2.text)
request異常:python
import requests try: url="http://www.google.com/" response=requests.get(url,timeout=5) print(response.status_code) print(response.text) except requests.exceptions.ConnectionError as e: print(e) #查看錯誤信息 print("over")
requests代理:json
import requests proxies={"http":"http://10.36.132.56:808", "https":"http://10.36.132.56:808"} #{"http":"用戶名:密碼@10.36.132.41:808"}) print(requests.get("http://www.baidu.com",proxies=proxies).text)
requests其它功能:api
''' import requests import chardet r=requests.get("http://www.baidu.com") print(chardet.detect(r.content)) #查看編碼 r.encoding=chardet.detect(r.content)['encoding'] #檢測網頁的編碼 print(r.text) print(r.history) print(r.url) ''' import requests user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' #模擬一個計算機 headers={'User-Agent':user_agent} #代理 r=requests.get("http://www.baidu.cn",headers=headers) print(r.headers) print(r.headers["Server"])