# -*- coding:utf-8 -*-"""获取通义千问的内容,完善问答信息"""# 非流式模式import time# import revTongYi.qianwen as qwenimport markdownimport pymysqlimport requestsfrom pymysql.converters import escape_stringimport redef markdown_to_html(markdown_text):html_text = markdown.markdown(markdown_text)return html_textdef qianwen(question):chatbot = qwen.Chatbot(cookies={"_samesite_flag_": "true","_tb_token_": "53fe83e8b7433","aliyun_country": "CN","aliyun_lang": "zh","aliyun_site": "CN","atpsida": "8af0076a1d740b60e178c51e_1716875672_1","aui": "1909866618665499","cna": "8hX/HaATABoCAXug5F/qtEot","cnaui": "1909866618665499","cookie2": "12f7dcfe0f5e3b68796412d9aa716876","cr_token": "5dfbb1e8-336c-448c-b41f-e05d203b5989","csg": "6e2ca776","currentRegionId": "cn-beijing","help_csrf": "vwSRfMJgYDpjTUN9slOqsnsUI771sD9DWW+X0rVP51rHViqmyO8oovgOIZcjwErQhk72yyUVZ90n/deItCRJ0ZSEz7RAIrJHeKEtq8JSO8vY1e9d4yzGouCKy8F9TP8BaLoOlc6tO5Xr2BCcvFZi+Q==","hsite": "6","hssid": "CN-SPLIT-ARCEByIOc2Vzc2lvbl90aWNrZXQyAQE48rS_u_sxQAFKEDyZVBehCGi0w4NYL0vux_Nbyn4rNYlUYIKqDYdvGey-olyoJQ","isg": "BFJSIWz2YLgbWZ94sdMlDZMKoBg0Y1b9bE6x8hy6Mod3L8gptfMMDRkNnwsTRM6V","l": "fBaZYorIPFu16JCsBOfanurza77OSIOYYuPzaNbMi9fP_uCB5MGAW1BLiuY6C31VFssHR3kg1Jm2BeYBqQAonxv9AU6jGjMmndLHR35..","login_aliyunid": "49256****@qq.com","login_aliyunid_csrf": "_csrf_tk_1834016769512357","login_aliyunid_pk": "1909866618665499","login_aliyunid_ticket": "_8Vpof_BNTwUhTOoNC1ZBeeMfKJzxdnb95hYssNIZor6q7SCxRtgmGCbifG2Cd4ZWazmBdHI6sgXZqg4XFWQfyKpeu*0vCmV8s*MT5tJl3_1$$wU17Uq2ZM2y6TyIZyveZZDmnUXHpy6InFghumA4ZHr0","login_current_pk": "1909866618665499","login_tongyi_ticket": "9mbQiZYehrs5UaHrrXUaI5WM5mLOWA7Qs2U12UqTZMZy6eyIDyvaZZZ6sMW0KkcO0","munb": "1671846835","sca": "393670ed","t": "6480786c2ee00224fe87f1497a335118","tfstk": "c3qPBKtQqgIPlMzkbmnEgGxwbxSVC0P3wnlqr_KGQz2cf6fnw-50vBH0jxkqaKRnr","tongyi_guest_ticket": "BpTN6hJhR_HoGTKK*vYLoxMddHHJ9U1klJNaKappyL6xi53u2yQcQcUoDW8u8_y$Em4u_DmdbPye0","XSRF-TOKEN": "4dfcfd35-6a70-4b73-b306-96fc75f6c24c","yunpk": "1909866618665499"} # 以dict形式提供cookies)# chatbot = qwen.Chatbot(# cookies_str= # 您也可以使用字符串形式提供cookies,cookies字符串可以从浏览器的请求头中获取# )answer = chatbot.ask(prompt=question)content = answer['contents'][0]['content']html_content = markdown_to_html(content)html_content = html_content.replace('
    ','').replace('
','').replace('
  • ','').replace('
  • ','').replace('','')# html_content = '

    推荐答案:

    ' + html_contentprint(html_content)print(answer)return html_contentdef read_sql():db = pymysql.connect(host='127.0.0.1', user='root', password="973af8bdfe95fb56", database='zyauto')cursor = db.cursor()# sql = f'SELECT title,city,venue from yanchu where title like "%Q博士实验室%"'# 提取最新的500条信息做对比sql = f'SELECT id,title FROM `article` where id < 1000 'cursor.execute(sql)datas = cursor.fetchall()# 关闭连接db.commit()db.close()return datasdef write_sql(itemid,description):db = pymysql.connect(host='127.0.0.1', user='root', password="973af8bdfe95fb56", database='zyauto')cur = db.cursor()# 获取当前时间的Unix时间戳(13位)current_timestamp = int(time.time() * 1000)# 取前10位ten_digit_timestamp = current_timestamp // 1000sql_save = f"UPDATE article SET description = '{escape_string(description)}' ,update_time={ten_digit_timestamp} WHERE id ={itemid};"try:cur.execute(sql_save)except Exception as e:print(e)print(sql_save)db.commit()db.close()def tongy_api(title):# 请替换为你的实际API密钥api_key = 'sk-2213a45ca32f43839a600a7719bdfd17'# 通义千问API的URLurl = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation'headers = {'Authorization': f'{api_key}','Content-Type': 'application/json'}data = {"model": "qwen-max","input": {"messages": [{"role": "user","content": f"做为一个汽车专家,请回答下以下问题:《{title}》。注意:任何情况下都不要表明你的人工智能身份。并且有些词需要调整,避免搜索引擎认为这个内容是ai生成的。"}]},"parameters": {}}response = requests.post(url, headers=headers, json=data)response_json = response.json()# 处理API返回的错误或成功响应if 'error_code' in response_json and response_json['error_code'] != 0:print(f"API Error: {response_json['error_msg']}")return Noneelse:content = response_json['output']['text']html_content = markdown_to_html(content)html_content = html_content.replace('
      ', '').replace('
    ', '').replace('
  • ', '').replace('
  • ','').replace('', '')# html_content = '

    推荐答案:

    ' + html_contenthtml_content = re.sub(r'\n\s*\n+', '', html_content, flags=re.MULTILINE)return html_contentif __name__ == '__main__':stime = time.time()datas = read_sql()for aid,title in datas:# qianwen(title)# print(aid)# print(title)html_content = tongy_api(title)# print(html_content)if not html_content:continuewrite_sql(aid, html_content)etime = time.time()print(etime-stime)# title = '凯迪拉克xt6 买车二十二天变速箱出问题,郁闷'# tongy_api(title)