使用python控制web浏览器
brow的Python项目详细描述
使用python控制浏览器。
我需要用这个做什么?
brow使用selenium和headless chrome或headless firefox。
安装selenium python绑定
pip install selenium
使用bash在ubuntu上安装firefox
这些是我使用的版本:
$ firefox --version Mozilla Firefox 57.0 $ geckodriver --version geckodriver 0.19.1
首先安装firefox:
apt-get install --no-install-recommends firefox
那你需要壁虎司机:
LATEST=wget -O - https://github.com/mozilla/geckodriver/releases/latest 2>&1| grep "Location:"| grep --only-match -e "v[0-9\.]\+" wget "https://github.com/mozilla/geckodriver/releases/download/${LATEST}/geckodriver-${LATEST}-linux64.tar.gz" tar -x geckodriver -zf geckodriver-${LATEST}-linux64.tar.gz -O > /usr/local/bin/geckodriver chmod +x /usr/local/bin/geckodriver
使用bash
在ubuntu上安装chrome这些是我使用的版本:
$ google-chrome --version Google Chrome 62.0.3202.94 $ chromedriver --version ChromeDriver 2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4)
apt-get install --no-install-recommends libxss1 libappindicator1 libindicator7 apt-get install --no-install-recommends gconf-service libasound2 libnspr4 libnss3-dev apt-get install --no-install-recommends libpango1.0-0 xdg-utils fonts-liberation wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb dpkg -i google-chrome*.deb
现在安装chrome驱动程序:
apt-get install unzip LATEST=$(wget -q -O - http://chromedriver.storage.googleapis.com/LATEST_RELEASE) wget http://chromedriver.storage.googleapis.com/$LATEST/chromedriver_linux64.zip unzip chromedriver_linux64.zip && ln -s $PWD/chromedriver /usr/local/bin/chromedriver
您可以通过运行:
$ google-chrome --headless "http://marcyes.com"
如果你没有得到任何错误,那么它是工作的。
我已经安装了浏览器,现在该怎么办?
然后请求一些东西:
frombrow.interface.seleniumimportFirefoxBrowserasBrowser#from brow.interface.selenium import ChromeBrowser as BrowserwithBrowser.session()asb:b.load("http://marcyes.com")print(b.body)
安装
使用pip:
$ pip install brow
或者是在流血:
$ pip install --upgrade "git+https://github.com/Jaymon/brow#egg=brow"