armbian docker Chrome_Linux+chrome+selenium浏览器截图

fd6ec80a1edb9d7ca863ce2905a206fb.png

软件安装

(1)根据操作系统安装对应的chrome浏览器软件和chrome driver驱动

(⚠️浏览器的版本和驱动的版本要对应上) 驱动地址:https://chromedriver.chromium.org/downloads

selenium打开浏览器

from selenium import webdriver
#设置useragent
UA ='Mozilla/5.0 (Win; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36'
#添加设备的UA和浏览器的长宽,分辨率
mobile_Emulation = {"deviceMetrics": {"width": WIDTH, "height": HEIGHT, "pixelRatio": PIXEL_RATIO},"userAgent": UA}
#虚拟界面
options = webdriver.ChromeOptions()
#添加UA
options.add_experimental_option('mobileEmulation', mobile_Emulation)
#headless无界面打开浏览器,如果使用自定义插件则需要head
# options.add_argument('--headless')
#不使用gpu
options.add_argument('--disable-gpu')
#不启动沙盒
options.add_argument('--no-sandbox')
#docker中没办法启动较大的页面,
options.add_argument('--disable-dev-shm-usage')
#指定cache的存放路径
options.add_argument('--disk-cache-dir=./chrome_cache')
#添加自定义插件,讲platform转成win/mac(这里逻辑是UA='Win',platform='Win';UA='Mac',platform='mac',UA='iphone',platform='iphone')
options.add_extension('./platform2Mac/platformChange.crx')
driver_path = './chromedriver83'
driver = webdriver.Chrome(executable_path=driver_path,chrome_options=options)
driver.get(url)
#打印浏览器的platform和页面的html
print(driver.execute_script("return navigator.platform"))
print(driver.page_source)
#将浏览器第一屏截图,保存为jpg
driver.get_screenshot_as_file('*.jpg')
driver.close()

tips:

  1. options.add_argument('--headless'),无界面打开浏览器,在linux中会使用这种情况。

linux中也可以使用有界面,则需要给linux添加虚拟界面

下载安装xvfb虚拟界面 sudo apt-get install xvfb pip install pyvirtualdisplay

from pyvirtualdisplay import Display
display = Display(visible=0, size=(1600, 900))
display.start()
#进行以上打开浏览器截图等操作
display.stop()

但由于项目需要多线程操作打开浏览器时,display.stop()无法关闭Xvfb进程号,导致进程堆积。解决办法:启动脚本时打开虚拟界面,脚本结束虚拟界面关闭。

xvfb-run python3 **.py

2.options.add_argument('--no-sandbox')

如果出现chrome的版本对应不上的报错信息,检查是否没有添加沙盒关闭。

3.options.add_argument('--disk-cache-dir=./chrome_cache')

chrome打开会加载很多的cache,如果在docker中运行,会占满docker的内存,导致脚本无法启动,此时可以将chache挂载出来。

docker中打开的话,检查一下 /var/cache/fontconfig。会存放很多浏览器打开页面的字体缓存,及时清理,不然会影响脚本的启动。

docker中的/tmp也需要关注一下。

4.options.add_extension('./platform2Mac/platformChange.crx')

最头痛的就是这个给浏览器添加插件问题,此处解决的问题是,有些html会判断浏览器的platform是PC还是ios,区别 展示html界面,这种就需要将linux上的浏览器模拟成手机端的浏览器,只改Useragent是无法解决问题的,需要在打开浏览器的时候加载插件。

chrome浏览器自定义插件-platform

1.需要两个文件

76673a26ce2e3d9d195f595bee1ef1e9.png

2.在chrome浏览器加载有这两个文件的目录,则会加载进浏览器这个插件

e6d6c5f9a3e1a16146fb1941fc3be83c.png

3.python代码中使用的时候需要打包成crk的文件,将刚才目录进行打包,生成相应的*.crk文件

2f591419c4af7c5d011a298868ef3e1f.png

4.文件代码

contentscript.js

// see https://stackoverflow.com/questions/23202136/changing-navigator-useragent-using-chrome-extension
var actualCode =  '(' + function() {
    // Get the correct platform for navigator.userAgent 
    var candidates = ["Mac","Win","Linux","Android", "iPhone", "iPod", "iPad"];
    var modifiedPlatform;
    for(var maybe of candidates) {
        if(navigator.userAgent.match(maybe)) {
            modifiedPlatform = maybe;
            break;
        }
    }

    if(!modifiedPlatform) {
        return;
    }

    // Reset platform
    Object.defineProperty(navigator, 'platform', {
        get: function(){
            return modifiedPlatform;
        }});
} + ')();';

// Inject custom js
var s = document.createElement('script');
s.textContent = actualCode;
document.documentElement.appendChild(s);
s.remove();

maniftest.json

{
    "name": "Simulate Device Hack",
    "version": "1.0",
    "description": "Modify the navigator.platform while simulating a mobile viewport",
    "manifest_version": 2,
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["contentscript.js"],
            "run_at": "document_start"
        }
    ]
  }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值