import requests import json class HttpCommon: def get(self,url,encode): headers = { "Connection": "keep-alive", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "User-Agent": "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11", "Content-Type": "application/x-www-form-urlencoded; charset="+encode.upper(), "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,zh;q=0.9" } res=requests.get(url,headers=headers) res.encoding=encode return res.text def post(self,url,param,encode): headers = { "Connection": "keep-alive", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "User-Agent": "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11", "Content-Type": "application/x-www-form-urlencoded; charset="+encode.upper(), "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,zh;q=0.9" } res = requests.post(url, data=json.dumps(param), headers=headers) res.encoding = encode return res.text def postjson(self,url,param): headers = { "Connection": "keep-alive", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "User-Agent": "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11", "Content-Type": "application/json", "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,zh;q=0.9" } # param 参数为 {"appid": "shpt","appsecret": "7e03a331-0407-4591-ba7f-75ee08756d4f"} res = requests.post(url, data=json.dumps(param), headers=headers) res.encoding = "utf-8" return res.text
python 基于 requests 发送 HTTP 请求
最新推荐文章于 2024-04-15 17:34:13 发布