PhantomJS is headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG. Within Python itself, there is module called Selenium. Selenium provide a way to comunicate with PhantomJS that originally build to serve JavaScript. PhantomJS is popular for it headless browser with javascript support. That why in this post, I'll show you how to headlessly browse the internet with PhantomJS and Selenium.
Install Selenium
Before installing, ensure your python is 3.4 or above to meet selenium requirement.
Now, install it with pip
pip install selenium
Install PhantomJSOn Mac you can install it with :
brew install phantomjs
On Windows or Linux, get PhantomJS from their site
Testing Selenium PhantomJS
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.get('https://python.org')
html = driver.page_source
print(html)
Importing selenium module, with :
from selenium import webdriver
Creating a webdriver object for PhantomJS
driver = webdriver.PhantomJS()
If you are on a Windows machine you can specify the path to the phantomjs executable:
driver = webdriver.PhantomJS("C://phantomjs.exe")
driver.get("http://python.org/")
If you don't want to write PhantomJS path every time you run the code, you could add phantomjs to your Windows PATH.
Follow this tutorial to do that :
Add Program to Windows PATHSimilar Links Python Windows Wheel Libs More Python Tutorials Sentdex Youtube Python | Official Website Stackoverflow | Place to ask
Comments
Post a Comment