requests文件_json:
#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异常:
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代理:
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其它功能:
''' 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"])