chatgpt的时代,先用gpt3来做一个哪里都可以接入的代码吧

from aiowebsocket.converses import AioWebSocket
import json
import requests
import time
from tkinter import *
import random
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from seleniumwire import webdriver
# from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
import re
from selenium.webdriver.common.by import By
chrome_options = webdriver.ChromeOptions()
browser = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
# 清除所有cookie
browser.delete_all_cookies()
browser.get('https://chat.forchange.cn')
time.sleep(5)
num = 0
while True:
input_add = browser.find_element(By.XPATH, r'//*[@id="chatgpt"]/div/div[1]/main/div[2]/form/div/div[2]/textarea')
input_word = input(str(num * 2 + 1) + '你要说到话:')
if input_word == '退出':
break
input_add.send_keys(input_word)
browser_click = browser.find_element(By.XPATH, r'//*[@id="chatgpt"]/div/div[1]/main/div[2]/form/div/div[2]/button')
browser_click.click()
time.sleep(5)
do_find = False
while do_find == False:
try:
try:
answer_error = browser.find_element(By.XPATH,
r'//*[@id="chatgpt"]/div/div[1]/main/div[1]/div/div/div/div[' + str(
num * 2 + 2) + ']/div/div[2]/div')
if answer_error.text == 'sorry,当前访问人数太多,请稍后再试!':
print('现在网络不好,即将点击重试按钮')
time.sleep(5)
retry_button = browser.find_element(By.XPATH,
r'//*[@id="chatgpt"]/div/div[1]/main/div[2]/form/div/div[1]/button')
retry_button.click()
except Exception as e:
print('没找到这个错误到时候,是不是能找到下面到情况')
time.sleep(10)
answer = browser.find_element(By.XPATH, r'//*[@id="chatgpt"]/div/div[1]/main/div[1]/div/div/div/div[' + str(
num * 2 + 2) + ']/div/div[2]/div')
if len(answer.text) != 0:
# print(num + 1, '你:', input_word)
print(str(num * 2 + 2) + '机器人:',answer.text)
do_find = True
num += 1
except Exception as e:
print(e)
time.sleep(5)
browser.close()

看看结果:

1你要说到话:请给出2022最伟大的人物
2机器人: 2022年最伟大的人物可能是美国总统唐纳德·特朗普,他在2022年将继续推动美国经济增长,改善美国的外交关系,并继续推动全球和平。
3你要说到话:

 

还不错哦,将它打包成方法,结果return就可以在任何地方接入了

posted @ 2023-02-14 21:39  星涅爱别离  阅读(118)  评论(1编辑  收藏  举报