Loading... [网址](https://tongyi.aliyun.com/qianwen/) 返回的数据也是一条一条返回的,以为是websocket,仔细看后,并不是的 ![image.png](http://type.zimopy.com/usr/uploads/2024/06/882128795.png) 直接给代码, ```python import requests # 请求的URL url = "https://qianwen.biz.aliyun.com/dialog/conversation" # 请求头信息 headers = { "Content-Type": "application/json", "Cookie": "_samesite_flag_=true;", "Origin": "https://tongyi.aliyun.com", "Pragma": "no-cache", "Priority": "u=1, i", "Referer": "https://tongyi.aliyun.com/qianwen/?sessionId=11693502a5b6422a0146", "Sec-Ch-Ua": "\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Google Chrome\";v=\"126\"", "Sec-Ch-Ua-Mobile": "?0", "Sec-Ch-Ua-Platform": "\"macOS\"", "Sec-Fetch-Dest": "empty", "Sec-Fetch-Mode": "cors", "Sec-Fetch-Site": "same-site", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36", "X-Platform": "pc_tongyi", "X-Xsrf-Token": "6d959885-bdc1-4ca" } # 请求体参数 data = { "model": "", "action": "next", "mode": "chat", "userAction": "chat", "requestId": "a0a4496193d04b6b98c29", "sessionId": "11693502a5b644b98d52db55", "sessionType": "text_chat", "parentMsgId": "f476119801d64bddb46656966", "contents": [ { "content": "当前市场环境如何", "contentType": "text", "role": "user" } ], "params": { "fileUploadBatchId": "3b0afae7d55a42c2aae83d87a41199d1", "agentId": "" } } # 发送POST请求,并使用stream参数 response = requests.post(url, headers=headers, json=data, stream=True) # 检查响应状态 if response.status_code == 200: # 持续读取流式数据 for line in response.iter_lines(): if line: # 对每一行数据进行处理 print(line.decode('utf-8')) else: print(f"请求失败,状态码: {response.status_code}") ``` 但是这里只是一个会话的,如果需要每次都是新的并删除上一个聊天就自己摸索,因为要更新session信息等 最后修改:2024 年 06 月 28 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏