开发者社区> 问答> 正文

百炼流程管理脚本节点一直返回 "scriptResult": {}

意图:工作流通过脚本节点抓取www.jin10.com的资讯,给到大模型做进一步资讯加工。

情况:在Window本地python上执行代码成功了,不过代码带有bs4,脚本节点不支持,改用了requests库代码,输出结果一直都是"scriptResult": {}

求解:问题是出在哪里?应该怎么写才成功?
以下是截图、电脑本地成功运行的代码和脚本节点的代码。

1111111.png

window电脑命令提示符运行成功的python文件代码:

import requests
import re

def extract_info_from_jin10():
url = "https://www.jin10.com"
try:
response = requests.get(url)
response.encoding = 'utf-8'

    # 提取时间
    time_pattern = re.compile(r'<div data-v-0bb4ed06="" class="item-time">(.*?)</div>')
    time_match = time_pattern.search(response.text)
    time = time_match.group(1).strip() if time_match else None
    # 提取标题
    title_pattern = re.compile(r'<b data-v-0bb4ed06="" class="right-common-title">(.*?)</b>')
    title_match = title_pattern.search(response.text)
    title = title_match.group(1).strip() if title_match else None
    # 提取内容
    content_pattern = re.compile(r'<div.*?class="flash-text">(.*?)</div>', re.DOTALL)
    content_match = content_pattern.search(response.text)
    content = content_match.group(1).strip() if content_match else None
    return time, title, content
except Exception as e:
    print(f"发生错误:{e}")
    return None, None, None

if name == "main":
print("开始执行脚本")
time, title, content = extract_info_from_jin10()
if time and title and content:
print(f"时间:{time}\n标题:{title}\n内容:{content}")
else:
print("未成功提取信息。")


以下是工作流脚本节点的代码:

def extract_time_from_jin10():
url = "https://www.jin10.com"
try:
response = requests.get(url)
response.encoding = 'utf-8'
time_pattern = re.compile(r'

(.*?)
')
time_match = time_pattern.search(response.text)
time = time_match.group(1).strip() if time_match else None
return {"time": time} # 将时间包装在字典中返回
except Exception as e:
return {"time": None} # 如果出现错误,返回包含 None 的字典

展开
收起
1201160656883245 2024-10-05 22:02:13 13 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载