ToB企服应用市场:ToB评测及商务社交产业平台

标题: 远程ubuntu 20.04服务器运行python+selenium+pyautogui 爬虫解决方案 [打印本页]

作者: 羊蹓狼    时间: 2024-8-28 21:03
标题: 远程ubuntu 20.04服务器运行python+selenium+pyautogui 爬虫解决方案
配景
   已经开辟了一套基于python+selenium+pyautogui的爬虫脚本,通过mobaxterm远程Ubuntu服务器,让脚本在服务器上一直运行,必要用到虚拟图形界面
  1、安装Google Chrome:

起首,利用wget命令下载Google Chrome的最新稳定版.deb包:
  1. wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
复制代码
然后,利用dpkg命令安装下载的.deb包:
  1. sudo dpkg -i google-chrome-stable_current_amd64.deb
复制代码
如果在安装过程中遇到依靠题目,可以利用以下命令来解决:
  1. sudo apt-get install -f
复制代码
如果有以下类似报错:
   dependency problems - leaving unconfigured Processing triggers for
mime-support (3.64ubuntu1) … Errors were encountered while processing:
google-chrome-stable
  执行以下命令,即可以解决报错
  1. apt --fix-broken install ./google-chrome-stable_current_amd64.deb
复制代码
安装完成后,可以通过运行以下命令来查抄Chrome的版本:
google-chrome --version
2、安装ChromeDriver:

访问 https://googlechromelabs.github.io/chrome-for-testing/#stable 这个地址去下载更新的chromedriver
  1. wget  https://storage.googleapis.com/chrome-for-testing-public/127.0.6533.119/linux64/chromedriver-linux64.zip
复制代码
然后到目录中执行:
  1. unzip chromedriver-linux64.zip
  2. cd chromedriver-linux64
  3. mv chromedriver /usr/bin
复制代码
3、安装虚拟表现器

利用 Xvfb(X Virtual Framebuffer)可以模拟一个虚拟的表现环境,这对于必要图形界面但不必要实际表现输出的自动化脚本非常有效。以下是在 Ubuntu 服务器上设置 Xvfb 的步调:
利用 Xvfb 的好处是,你可以在没有物理表现器或 X11 转发的环境下运行必要图形界面的应用步伐。这对于服务器环境或持续集成/持续摆设(CI/CD)流程非常有效。
运行报错

运行脚本报错题目:
   buntu报错selenium.common.exceptions.SessionNotCreatedException:
Message: session not created: Chrome failed to start: exited normally.
(session not created: DevToolsActivePort file doesn’t exist) (The
process started from chrome location
/snap/chromium/2925/usr/lib/chromium-browser/chrome is no longer
running, so ChromeDriver is assuming that Chrome has crashed.)
  可能是代码中 chromedriver_path 的位置配错了
查看 chromedriver 的位置利用 which chromedriver
其他

   虚拟框架(如 Xvfb)和 ubuntu-desktop、xfce4、lxde等图形界面有啥区别?
  Xvfb 和 ubuntu-desktop 是两个差别的组件,它们在功能和用途上有显着的区别:
主要区别:

Xvfb 是一个无头(headless)的解决方案,适用于必要图形界面支持但不必要表现给用户看的应用步伐,而 ubuntu-desktop 是一个完整的桌面环境,提供了用户可以直接交互的图形界面。
一开始把这两者搞混了,以为只要安装了图形界面就可以运行爬虫了,结果打开的是本机的谷歌浏览器,利用了x11转发,只要一关闭mobaxterm,脚本就报错。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4