当前位置: 首页 > news >正文

【java执行python】

java执行python脚本

java代码如下

public String executePythonScript(String scriptPath, String... args) {try {// 构建命令List<String> command = new ArrayList<>();command.add("python");command.add(scriptPath);command.addAll(Arrays.asList(args));ProcessBuilder pb = new ProcessBuilder(command);pb.redirectErrorStream(true); // 合并错误流和输出流// 启动进程long startTime = System.currentTimeMillis();Process process = pb.start();// 读取输出StringBuilder output = new StringBuilder();try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {String line;while ((line = reader.readLine()) != null) {output.append(line).append("\n");}}// 等待进程完成int exitCode = process.waitFor();long endTime = System.currentTimeMillis();System.out.println("Python脚本执行时间: " + (endTime - startTime) + "ms");if (exitCode != 0) {throw new RuntimeException("Python脚本执行失败,退出码: " + exitCode);}return output.toString();} catch (IOException | InterruptedException e) {throw new RuntimeException("执行Python脚本出错", e);}
}

python脚本

import argparse
import os
import sys
from urllib.parse import urlparse
from urllib.request import urlretrievefrom PIL import Image
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManagerclass ScreenshotTool:def __init__(self, headless=True):"""初始化截图工具:param headless: 是否使用无头模式"""self.headless = headlessself.driver = self._init_webdriver()def _init_webdriver(self):"""初始化 Selenium WebDriver"""chrome_options = Options()if self.headless:chrome_options.add_argument("--headless")chrome_options.add_argument("--disable-gpu")chrome_options.add_argument("--no-sandbox")chrome_options.add_argument("--disable-dev-shm-usage")chrome_options.add_argument("--window-size=1920,1080")# 使用 webdriver-manager 自动管理 ChromeDriverservice = Service(ChromeDriverManager().install())driver = webdriver.Chrome(service=service, options=chrome_options)return driverdef capture_url(self, url, output_path="screenshot.png"):"""捕获网页截图:param url: 网页URL:param output_path: 输出文件路径:return: 截图保存路径"""try:self.driver.get(url)# 等待页面加载(简单实现,生产环境应使用更智能的等待方式)self.driver.implicitly_wait(10)# 获取页面实际高度并设置窗口大小total_height = self.driver.execute_script("return document.body.scrollHeight")self.driver.set_window_size(1920, total_height)# 截图并保存self.driver.save_screenshot(output_path)print(f"截图已保存到: {os.path.abspath(output_path)}")return output_pathexcept Exception as e:print(f"截图失败: {str(e)}", file=sys.stderr)return Nonedef capture_local_image(self, image_path, output_path="screenshot.png"):"""打开本地图片并保存(主要用于统一接口):param image_path: 本地图片路径:param output_path: 输出文件路径:return: 输出文件路径"""try:img = Image.open(image_path)img.save(output_path)print(f"图片已保存到: {os.path.abspath(output_path)}")return output_pathexcept Exception as e:print(f"处理本地图片失败: {str(e)}", file=sys.stderr)return Nonedef download_and_capture(self, url, output_path="screenshot.png"):"""下载远程图片并保存:param url: 图片URL:param output_path: 输出文件路径:return: 输出文件路径"""try:# 临时文件路径temp_file = "temp_download_image"# 下载图片urlretrieve(url, temp_file)# 验证是否为有效图片try:with Image.open(temp_file) as img:img.save(output_path)print(f"图片已保存到: {os.path.abspath(output_path)}")return output_pathfinally:# 删除临时文件if os.path.exists(temp_file):os.remove(temp_file)except Exception as e:print(f"下载和处理图片失败: {str(e)}", file=sys.stderr)return Nonedef close(self):"""关闭 WebDriver"""if self.driver:self.driver.quit()def is_url(self, input_str):"""检查输入是否是URL"""try:result = urlparse(input_str)return all([result.scheme, result.netloc])except ValueError:return Falsedef capture(self, source, output_path="screenshot.png"):"""根据输入源进行截图或保存图片:param source: 图片源(URL或本地路径):param output_path: 输出文件路径:return: 输出文件路径"""if self.is_url(source):# 检查URL是否是图片if source.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):return self.download_and_capture(source, output_path)else:return self.capture_url(source, output_path)else:# 本地文件路径return self.capture_local_image(source, output_path)def main():# 设置命令行参数解析parser = argparse.ArgumentParser(description="网页/图片截图工具")parser.add_argument("source", help="图片或网页的URL/本地路径")parser.add_argument("-o", "--output", help="输出文件路径", default="screenshot.png")parser.add_argument("--visible", help="使用可见浏览器模式", action="store_true")args = parser.parse_args()# 初始化截图工具tool = ScreenshotTool(headless=not args.visible)try:# 执行截图result = tool.capture(args.source, args.output)if not result:sys.exit(1)finally:tool.close()if __name__ == "__main__":main()

使用说明

安装依赖
在运行脚本前,需要安装以下依赖:

bash
pip install selenium pillow webdriver-manager

使用示例
截取网页并保存为 screenshot.png:

bash
python screenshot.py https://www.example.com

截取网页并指定输出文件名:

bash
python screenshot.py https://www.example.com -o example.png
使用可见浏览器模式截图:

bash
python screenshot.py https://www.example.com --visible

处理本地图片文件:

bash
python screenshot.py /path/to/local/image.jpg

处理远程图片文件:

bash
python screenshot.py https://example.com/image.jpg

功能说明

  • 支持网页截图和图片处理(本地和远程)

  • 自动检测输入是URL还是本地路径

  • 自动区分网页和图片URL

  • 使用无头浏览器模式(默认)或可见浏览器模式(–visible)

  • 自动管理ChromeDriver版本

  • 错误处理和清理资源

注意事项

  • 确保系统已安装Chrome浏览器

  • 首次运行会自动下载合适的ChromeDriver

  • 对于复杂的网页,可能需要调整等待时间或添加更智能的等待逻辑


文章转载自:

http://VWenoHRV.wdprz.cn
http://nbvjbxpi.wdprz.cn
http://HuY8nD3B.wdprz.cn
http://p8nnu0Uh.wdprz.cn
http://RJKe4BWk.wdprz.cn
http://sxrBnNDR.wdprz.cn
http://Ra2BTc7n.wdprz.cn
http://ixPoMTZD.wdprz.cn
http://Kxg3oWWQ.wdprz.cn
http://nHIvq5t8.wdprz.cn
http://CdzT9UfM.wdprz.cn
http://sKamhP5P.wdprz.cn
http://KeinN05S.wdprz.cn
http://11rNeRYw.wdprz.cn
http://F28YRS01.wdprz.cn
http://bq8jPpb3.wdprz.cn
http://M1lOms4j.wdprz.cn
http://bx9vqKNa.wdprz.cn
http://RDYDn4zt.wdprz.cn
http://2wLwfEQB.wdprz.cn
http://hNFMZzqg.wdprz.cn
http://c9naAePM.wdprz.cn
http://Dt2d9Z2D.wdprz.cn
http://caEau6xD.wdprz.cn
http://5kM24PWD.wdprz.cn
http://8k8rN3oH.wdprz.cn
http://QJUfxg5N.wdprz.cn
http://woEyPrbB.wdprz.cn
http://6bEgok8b.wdprz.cn
http://cfUVwzmj.wdprz.cn
http://www.dtcms.com/a/372401.html

相关文章:

  • 【数据结构】强化训练:从基础到入门到进阶(1)
  • 三.动态规划算法
  • Maya绑定:驱动关键帧动画案例,小球穿过自动门
  • Android影像基础--cameraAPI2核心流程
  • Ollama Python库的使用
  • 【数据结构入门】排序算法(3):了解快速排序
  • 运筹学——对偶问题的建模,以及它的基本性质
  • 【PyTorch】图像多分类
  • 【0基础PS】PS工具详解--渐变工具
  • FPGA数据流分析
  • 用最简单的方法讲通Java快慢指针思想
  • 棱镜的技术加持:线扫相机如何同时拍RGB和SWIR?
  • [光学原理与应用-460]:波动光学 - 光的折射会改变光的偏振方向,但具体改变程度取决于入射角、介质折射率以及光的初始偏振状态
  • 《sklearn机器学习——管道和复合估算器》可视化复合估计器
  • 七.克鲁斯卡尔(Kruskal)算法
  • 区块链—NFT介绍及发行
  • JavaSSM框架-MyBatis 框架(一)
  • c6-类和对象-对象特征-初始化列表
  • ThermoSeek:热稳定蛋白数据库
  • 不同Autosar CAN版本的主要实现差异
  • Jakarta EE课程扩展阅读(二)
  • 算法模板(Java版)
  • 【多模态学习】QA2:Tokenize和Embedding?BPE算法?交叉熵损失函数?
  • ViT学习
  • 【Java实战㉚】深入MyBatis:从动态SQL到缓存机制的进阶之旅
  • 腾讯云EdgeOne免费套餐:零成本开启网站加速与安全防护
  • Cookie-Session 认证模式与Token认证模式
  • Redis哨兵模式在Spring Boot项目中的使用与实践
  • [工作表控件13] 签名控件在合同审批中的应用
  • 【图像理解进阶】MobileViT-v3核心技术解析和应用场景说明