随笔- 310  文章- 1  评论- 0  阅读- 86066 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#coding:utf-8
import requests
import json
try:
    from urllib.parse import urlencode
except:
    from urllib import urlencode
  
session = requests.Session()
  
def serialize(params):
    if isinstance(params,dict):
        return urlencode(params)
 
def request_data(host,path,params,method="GET",body={}):
    try:
        method = method.upper()
        headers = {
            'Content-Type': 'application/json',           
        }
        if method=="POST":
            request_params={}
            for k,v in params.items():
                request_params[k]=v
            serialize_params= serialize(request_params)
            url="{}{}?{}".format(host,path,serialize_params)
            print('curl -X POST -H "Content-Type:application/json" "{}" -d \'{}\' '.format(url,json.dumps(body)))
            response = session.post(url,data=json.dumps(body),headers=headers)
        else:
            request_params = {}
            for k,v in params.items():
                request_params[k]=v
            serialize_params= serialize(request_params)
            url="{}{}?{}".format(host,path,serialize_params)
            print('curl -X GET "{}"'.format(url))
            response = session.get(url,headers=headers)
        if response.status_code == 200:
            res = json.loads(response.text)
            return res
        return []
    except Exception as e:
        print(e)
        return []
if __name__ == "__main__":
    host="www.baidu.com"
    path="/s"
    params={
    "q":"test"
    }
    request_data(path)
    

  

 posted on   boye169  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示