背景
最近一直在搞論壇的爬蟲辅鲸。爬著爬著,突然遇到一個(gè)論壇的反爬蟲機(jī)制比較強(qiáng)腹殿。例如:http://bbs.nubia.cn/forum-64-1.html独悴。當(dāng)訪問這個(gè)頁面時(shí),第一次返回的不是html頁面锣尉,而是加密后的js內(nèi)容刻炒,然后寫入cookie,等待設(shè)置好的時(shí)間自沧,然后跳轉(zhuǎn)到真正的頁面落蝙。 如下圖:
- 想到的方案:
- 分析加密的js,看怎么計(jì)算出的cookie暂幼,是否有規(guī)律可以生成該cookie等筏勒,然后每次訪問時(shí)帶上此cookie即可。
- 使用Pypisder自帶的PhantomJs腳本旺嬉,以PhantomJs的方法執(zhí)行這個(gè)加密的JS管行,然后獲取html的內(nèi)容。
- 使用Selenium +WebDriver + Headless Chrome的方式獲取html的內(nèi)容邪媳。
- 使用puppeteer + Headless Chrome獲取html的內(nèi)容捐顷。
- 分析方案:
分析加密的js不是件容易的事荡陷,要破解加密方法等難度相對(duì)較大,時(shí)間成本有限迅涮,暫時(shí)放棄废赞。
本打算使用pyspider自帶的phanthomjs方式,結(jié)果是phanthoms在訪問上面的url時(shí)叮姑,一直處于卡死的狀態(tài);而且phantomjs的作者已經(jīng)放棄維護(hù)它了唉地,對(duì)于bug的修復(fù)和新的js語法支持力度不夠。(需要進(jìn)一步分析為什么會(huì)卡死传透?)
最終想到了selenium+webdriver+headlesschrome組合神器耘沼。想著如果能使用seleniu+chrome實(shí)現(xiàn)爬取動(dòng)態(tài)頁面,搭配pyspider爬取動(dòng)態(tài)頁面豈不是很爽朱盐。chrome是真正的瀏覽器群嗤,支持js特性比較全面,能真實(shí)的模擬用戶請(qǐng)求兵琳,而且Chrome官方也出了針對(duì)headless chrome的api(node版本)狂秘,api的可靠性,支持chrome特性的力度都非常的好躯肌。
puppeteer 是nodejs版本的api赃绊, 對(duì)nodejs不熟悉,暫時(shí)先忽略羡榴。
Chrome
Chrome瀏覽器從59版本之后開始支持headless模式碧查,用戶可以在無界面下進(jìn)行各種操作,例如:截圖校仑、把html輸出PDF等等忠售。MAC、Linux迄沫、Windows均有對(duì)應(yīng)的Chrome稻扬,請(qǐng)根據(jù)平臺(tái)進(jìn)行下載、安裝羊瘩。更多headless chrome的用法可以參考:headless chrome用法
Selenium
Selenium是一個(gè)自動(dòng)瀏覽器泰佳。雖然官方文檔說Selenium是一個(gè)自動(dòng)瀏覽器,但它并不是真正的瀏覽器尘吗,它只是可以驅(qū)動(dòng)瀏覽器而已逝她。Selenium是通過WebDriver來驅(qū)動(dòng)瀏覽器的,每個(gè)瀏覽器都有對(duì)應(yīng)的WebDriver睬捶。如果我們要用Chrome黔宛,就需要下載Chrome的WebDriver。
Selenium有很多語言版本的擒贸,你可以選擇使用java, python等版本的臀晃。我這里選擇使用python版的觉渴。python版本的selenium可以通過pip進(jìn)行安裝:pip install selenium
下載WebDriver時(shí)需要選擇對(duì)應(yīng)的平臺(tái),比如我是在win上跑徽惋,下載的就是windows平臺(tái)的案淋。
配置PATH
把下載的WebDriver放在PATH環(huán)境變量可以加載到的位置,這樣在程序中不用指定webDriver险绘,會(huì)方便很多踢京。
Python版本的Headless Chrome Web Server
- pyspider利用phantomjs爬取js動(dòng)態(tài)頁面的原理:
pyspider搜索PATH中的phantomjs命令,然后使用phantomsjs去執(zhí)行phantomjs_fetcher.js隆圆,從而啟動(dòng)一個(gè)監(jiān)聽固定port的web server服務(wù);當(dāng)在Handler中的self.crawl(xxx)方法中帶上fetch_type='js'參數(shù)時(shí)翔烁,pyspider便發(fā)請(qǐng)求給這個(gè)port渺氧,利用phantomjs去轉(zhuǎn)發(fā)請(qǐng)求,訪問js動(dòng)態(tài)頁面蹬屹,從而爬蟲動(dòng)態(tài)頁面侣背。
如果我想利用selenium+chrome爬取動(dòng)態(tài)頁面,也需要實(shí)現(xiàn)一個(gè)web server慨默,以便pyspider可以訪問它贩耐,利用它轉(zhuǎn)發(fā)請(qǐng)求。python版本的實(shí)現(xiàn)如下:
"""
selenium web driver for js fetcher
"""
import urlparse
import json
import time
import datetime
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['POST', 'GET'])
def handle_post():
if request.method == 'GET':
body = "method not allowed!"
headers = {
'Cache': 'no-cache',
'Content-Length': len(body)
}
return body, 403, headers
else:
start_time = datetime.datetime.now()
raw_data = request.get_data()
fetch = json.loads(raw_data, encoding='utf-8')
print('fetch=', fetch)
result = {'orig_url': fetch['url'],
'status_code': 200,
'error': '',
'content': '',
'headers': {},
'url': '',
'cookies': {},
'time': 0,
'js_script_result': '',
'save': '' if fetch.get('save') is None else fetch.get('save')
}
driver = InitWebDriver.get_web_driver(fetch)
try:
InitWebDriver.init_extra(fetch)
driver.get(fetch['url'])
# first time will sleep 2 seconds
if InitWebDriver.isFirst:
time.sleep(2)
InitWebDriver.isFirst = False
result['url'] = driver.current_url
result['content'] = driver.page_source
result['cookies'] = _parse_cookie(driver.get_cookies())
except Exception as e:
result['error'] = str(e)
result['status_code'] = 599
end_time = datetime.datetime.now()
result['time'] = (end_time - start_time).seconds
# print('result=', result)
return json.dumps(result), 200, {
'Cache': 'no-cache',
'Content-Type': 'application/json',
}
def _parse_cookie(cookie_list):
if cookie_list:
cookie_dict = dict()
for item in cookie_list:
cookie_dict[item['name']] = item['value']
return cookie_dict
return {}
class InitWebDriver(object):
_web_driver = None
isFirst = True
@staticmethod
def _init_web_driver(fetch):
if InitWebDriver._web_driver is None:
options = Options()
# set proxy
if fetch.get('proxy'):
if '://' not in fetch['proxy']:
fetch['proxy'] = 'http://' + fetch['proxy']
proxy = urlparse.urlparse(fetch['proxy']).netloc
options.add_argument('--proxy-server=%s' % proxy)
# reset headers, for now, do nothing
set_header = fetch.get('headers') is not None
if set_header:
fetch['headers']['Accept-Encoding'] = None
fetch['headers']['Connection'] = None
fetch['headers']['Content-Length'] = None
if set_header and fetch['headers']['User-Agent']:
options.add_argument('user-agent=%s' % fetch['headers']['User-Agent'])
# disable load images
if fetch.get('load_images'):
options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
# set viewport
fetch_width = fetch.get('js_viewport_width')
fetch_height = fetch.get('js_viewport_height')
width = 1024 if fetch_width is None else fetch_width
height = 768 * 3 if fetch_height is None else fetch_height
options.add_argument('--window-size={width},{height}'.format(width=width, height=height))
# headless mode
options.add_argument('--headless')
InitWebDriver._web_driver = webdriver.Chrome(chrome_options=options, port=10001)
@staticmethod
def get_web_driver(fetch):
if InitWebDriver._web_driver is None:
InitWebDriver._init_web_driver(fetch)
return InitWebDriver._web_driver
@staticmethod
def init_extra(fetch):
# maybe throw TimeOutException
driver = InitWebDriver._web_driver
if fetch.get('timeout'):
driver.set_page_load_timeout(fetch.get('timeout'))
driver.set_script_timeout(fetch.get('timeout'))
else:
driver.set_page_load_timeout(20)
driver.set_script_timeout(20)
# # reset cookie
# cookie_str = fetch['headers']['Cookie']
# if fetch.get('headers') and cookie_str:
# # driver.delete_all_cookies()
# cookie_dict = dict()
# for item in cookie_str.split('; '):
# key = item.split('=')[0]
# value = item.split('=')[1]
# cookie_dict[key] = value
# # driver.add_cookie(cookie_dict)
@staticmethod
def quit_web_driver():
if InitWebDriver._web_driver is not None:
InitWebDriver._web_driver.quit()
if __name__ == '__main__':
app.run('0.0.0.0', 9000)
InitWebDriver.quit_web_driver()
此實(shí)現(xiàn)參考pyspider源碼:tornado_fetcher.py和phantomjs_fetcher.js厦取。但是潮太,實(shí)現(xiàn)的功能不全,只實(shí)現(xiàn)了我需要的功能虾攻。諸如:設(shè)置cookie, 執(zhí)行js等并沒有實(shí)現(xiàn)铡买,您可以參考上面的實(shí)現(xiàn),實(shí)現(xiàn)自己的需求霎箍。
讓pyspider fetcher訪問Headless Chrome Web Server
- 先啟動(dòng)chrome web server奇钞,直接運(yùn)行:
python selenium_fetcher.py
即可,端口為9000 - 在啟動(dòng)pyspider時(shí)漂坏,指定
--phantomjs-proxy=http://localhost:9000
參數(shù)景埃,如:pyspider --phantomjs-proxy=http://localhost:9000
。