Using Phantomjs to capture and render a webpage after JS requires crawling a website recently. However, all pages are generated after JS rendering. the common crawler framework is not fixed, so I want to use Phantomjs to build a proxy.
Python calls Phantomjs and it seems that there is no ready-made third-party library (if any, please let me know). after walking around, we found that only pyspider provides a ready-made solution.
After a simple trial, I feel that pyspider is more like a crawler tool for beginners. it is like a mother-in-law, sometimes meticulous, and sometimes chattering.
Lightweight gadgets should be more popular. I am also selfish and can use them with my favorite BeautifulSoup, instead of learning PyQuery (pyspider is used to parse HTML ), it does not have to endure the bad experience of writing Python in a browser ).
So it took an afternoon to split the part of pyspider implementing the Phantomjs proxy into a small crawler module. I hope you will like it (thanks to binux !).
Preparations
Of course you need Phantomjs! (In Linux, it is best to use the supervisord daemon. you must keep Phantomjs in the enabled state when capturing it)
Start with phantomjs_fetcher.js in the project path: phantomjs phantomjs_fetcher.js [port]
Install tornado dependencies (the httpclient module of tornado is used)
Calling is super simple
From tornado_fetcher import Fetcher # Create a crawler> fetcher = Fetcher (user_agent = 'phantomj', # simulate the browser's User-Agent phantomjs_proxy = 'http: // localhost: 100 ', # phantomjs address poolsize = 10, # maximum number of httpclient async = False # synchronous or asynchronous) # start to connect to the proxy of Phantomjs and render JS. >>> Fetcher. phantomjs_fetch (url) # execute an additional JS script after the rendering is successful (use the function to pack it!) >>> Fetcher. phantomjs_fetch (url, js_script = 'Function () {setTimeout ("window. scrollTo (1000 )')