Python使用Adblock与Selenium和Firefox Webdriver [英] Python Using Adblock with Selenium and Firefox Webdriver

查看:389
本文介绍了Python使用Adblock与Selenium和Firefox Webdriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是通过Python使用Adblock Plus和Selenium。我已经能够达到它将加载扩展,但默认情况下,它不包括默认筛选器EasyList。这是我到目前为止:

  from selenium import webdriver 
from time import sleep
ffprofile = webdriver.FirefoxProfile()
adblockfile ='/Users/username/Downloads/adblock_plus-2.4-tb+fx+an+sm.xpi'
ffprofile.add_extension(adblockfile)
ffprofile.set_preference (extensions.adblockplus.currentVersion,2.4)
browser = webdriver.Firefox(ffprofile)
while(True):
browser.get(www.cnn.com)
sleep(5)

大部分代码都是从 http://selenium-python.readthedocs.org/en/latest/faq.html <实际上,Adblock Plus默认会添加EasyList,但是如果你设置了 extensions.adblockplus.currentVersion 首选项禁用更新/首次运行操作。我想你的目标是防止第一次运行的页面显示,但也阻止了数据存储的初始化。请注意,这里有更多的问题:即使Adblock Plus添加EasyList,它仍然需要一个未知的时间来下载。



更好的行动应该是初始化您的个人资料使用现有的 adblockplus / patterns.ini 文件。您可以使用EasyList和其他过滤器设置从常规的Firefox配置文件获取此文件,并将其复制到 /Users/username/Downloads/profilemodel/adblockplus/patterns.ini 。然后下面的应该工作:

pre $ fcprofile = webdriver.FirefoxProfile(/ Users / username / Downloads / profilemodel);


My goal is to use Adblock Plus with Selenium via Python. I have been able to get it to the point where it will load the extension, but by default it does not include the default filter "EasyList." Here is what I have so far:

 from selenium import webdriver
 from time import sleep
 ffprofile = webdriver.FirefoxProfile()
 adblockfile = '/Users/username/Downloads/adblock_plus-2.4-tb+fx+an+sm.xpi'
 ffprofile.add_extension(adblockfile)
 ffprofile.set_preference("extensions.adblockplus.currentVersion", "2.4")
 browser = webdriver.Firefox(ffprofile)
 while(True):
    browser.get("www.cnn.com")
    sleep(5)

Most of this code was ripped off of http://selenium-python.readthedocs.org/en/latest/faq.html

解决方案

Actually, Adblock Plus will add EasyList by default - but not if you set extensions.adblockplus.currentVersion preference to disable update/first-run actions. I guess that your goal was preventing the first-run page from showing up but it also prevented data storage initialization. Note that you have more issues here: even if Adblock Plus adds EasyList, it will still take an unknown time to download.

The better course of action should be initializing your profile with an existing adblockplus/patterns.ini file. You can get this file from your regular Firefox profile, with EasyList and other filter settings, and copy it to /Users/username/Downloads/profilemodel/adblockplus/patterns.ini. Then the following should work:

ffprofile = webdriver.FirefoxProfile("/Users/username/Downloads/profilemodel");

这篇关于Python使用Adblock与Selenium和Firefox Webdriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆