树莓派4B selenium + firefox + python3

入手了一个树莓派4B,想爬一些种子网站(手动滑稽)。运用往常的requests + lxml 一顿操作发现站点加了DDOS防护不可用,无法请求到真实页面。经常度娘一番查找,发现了selenium这么个玩意配合浏览器可以模拟浏览器行为。于是踩了大坑。。
selenium 可以配合 多种浏览器,其中最多的就是 chrome 和 firefox (网上教程也最多)
由于是phper,自然选择chrome,碍于基础知识太差,搜索了很多非arm架构下的教程,全部GG,经过一番google和度娘,遂成此文。
关于树莓派说一下几个坑:

  1. 树莓派的ARM架构,chrome + selenium 不再支持 ,不需要再去捣鼓了,此路完全不通 因此此文以 firefox 继续
  2. 树莓派的源,有很多的依赖装不上,此时,通过更换源,可以装上,这里大家还是建议自备梯子用官方源,能装大部分
  3. 搜教程 用树莓派做关键词先找教程。。

这里大家可以先看下这个文章:
http://www.knight-of-pi.org/python3-browser-tests-on-a-raspberry-pi-with-firefox-virtualdisplay-selenium-and-pytest/
本文大部分是重复这里的步骤

正文开始

  1. 根据教程安装依赖
    通过 apt search firebox 搜索 查看列表:
    firefox-esr
    Mozilla Firefox web browser - Extended Support Release (ESR)

    就是这货了
    执行 apt install firefox-esr 安装

  2. 根据教程安装依赖
    sudo apt-get install iceweasel xvfb -y
    sudo pip3 install selenium==2.53.6 pyvirtualdisplay pytest

  3. 安装驱动

tar -xf geckodriver-v0.19.1-arm7hf.tar.gz
rm geckodriver-v0.19.1-arm7hf.tar.gz
sudo chmod a+x geckodriver
sudo mv geckodriver /usr/local/bin/
  1. 测试
    python3 进入命令行后执行
 from selenium import webdriver
 from pyvirtualdisplay import Display
 display = Display(visible=0, size=(800, 600))
 display.start()
 profile = webdriver.FirefoxProfile()
 profile.native_events_enabled = False
 driver = webdriver.Firefox(profile)
 driver.set_page_load_timeout(60)

driver = webdriver.Firefox(profile) 这一步只要没有报错就没有问题了

可能存在的问题

  • 第二部分的安装依赖包可能装不上,个人测试 iceweasel 官方源装不上,换成中科大的就可以了
  • 报错selenium.common.exceptions.WebDriverException: Message: Can’t load the profile. Profile Dir: /tmp/tmpvrnv5olg If you specified 因为驱动版本的问题,或者是新安装的浏览器版本高于驱动了,这时候可以卸载掉 selenium,直接安装最新版。并且安装新版本的驱动程序,截止今日本文,驱动已更新至0.240版本,但不再提供arm的版本,需要自行编译,反正我是没编译成功,有成功的可以发出来分享下。。我这里是安装了较低的0.23版本:
    https://github.com/mozilla/geckodriver/releases/tag/v0.23.0
    重新安装后测试后成功

你可能感兴趣的:(树莓派)