Python爬虫-爬取ChatGPT接口进行聊天对话[代码现取现用]

在此推荐一下作者应用此方法编写一款基于Python的终端小程序软件:ChatTerminal!

Windows安装包云盘链接:ChatTerminal​​​​​​

from requests import *

import os, json, datetime

def GPT(InPut):

  api = "https://gpt.chatapi.art/backend-api/conversation"

  headers = {

    "authority": "gpt.chatapi.art",

    "method": "POST",

    "path": "/backend-api/conversation",

    "scheme": "https",

    "accept": "text/event-stream",

    "accept-encoding": "gzip, deflate, br",

    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",

    "authorization": "Bearer",

    "content-length": "236",

    "content-type": "application/json",

    "origin": "https://gpt.chatapi.art",

    "referer": "https://gpt.chatapi.art/",

    "sec-ch-ua": """Not?A_Brand";v="8", "Chromium";v="108", "Microsoft Edge";v="108""",

    "sec-ch-ua-mobile": "?0",

    "sec-ch-ua-platform": "Windows",

    "sec-fetch-dest": "empty",

    "sec-fetch-mode": "cors",

    "sec-fetch-site": "same-origin",

    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.42",

    "If-Modified-Since": f"Tue, 28 Jan 2020 12:{datetime.datetime.now().minute}:{datetime.datetime.now().second} GMT",

  }

  Json = {"action": "next",

        "messages": [{"id": "a2e292a1-59cc-4fca-81ed-5a8f8458f1be",

                      "role": "user",

                      "content": {"content_type": "text", "parts": [InPut]}}],

        "parent_message_id": "be81a331-eacf-45fd-bc47-6a6375ab2335",

        "model": "text-davinci-002-render"}

  RES = ""

  data = post(api, json=Json, headers=headers).text.replace("data: ", "")

  lis0 = data.split("\n")

  for i in lis0[::-1]:

    if i and i != '[DONE]':

        RES = i

        break

  try:

    Res = json.loads(RES)['message']['content']['parts'][0]

  except:

    print("Error:",RES)

    print("处理字典数据时发生错误.")

    Res="[Error]"

  return Res

print(GPT("地球半径是多少?"))

6fb7132045a14b17944e6b553554af75.png

f319c60364e843c498ba69c37b8f8b25.png 

c88f43e3d7f444fbb78ba28f47bb1616.png 

00c43590461246609b533826458c10c3.png 文章于2022/12/9发布,之后所调用接口可能无法使用导致程序失效!

 

 

 

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年3月5日 下午12:26
下一篇 2023年3月5日 下午12:27

相关推荐