You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
import requests
import json
import ast
def get_ai_code ( url , headers , data ) :
# 将字典转换为JSON字符串
payload = json . dumps ( data )
# 发送POST请求
response = requests . post ( url , headers = headers , data = payload )
# 检查响应状态码是否为200
if response . status_code != 200 :
# 如果状态码不是200, 则抛出异常或记录错误信息
print ( f " 请求失败,状态码: { response . status_code } " )
else :
# 打印响应的状态码和内容
message = json . loads ( response . text )
code = message [ " data " ] [ " code " ]
print ( code )
# 提取列表部分
list_part = code [ code . find ( ' [ ' ) : code . find ( ' ] ' ) + 1 ]
# 使用 ast.literal_eval 安全地解析字符串
data = ast . literal_eval ( list_part )
scent_channel = [ item . get ( " channelId " ) for item in data ]
play_time = [ item . get ( " time " ) for item in data ]
print ( message [ " data " ] [ " description " ] )
print ( message [ " data " ] [ " remark " ] )
return scent_channel , play_time
if __name__ == " __main__ " :
# 定义请求的URL
url = " https://ai.qiweiwangguo.com/api/conversation "
# 定义请求头, 包括Content-Type
headers = {
" Content-Type " : " application/json "
}
# 定义要发送的数据
data = {
" names " : " 桂花 " ,
" scents " : " 我想静静 " ,
" message " : " 请使用两种气味,调出一个可以缓解疲劳的气味。 "
}
get_ai_code ( url , headers , data )