安装包下载地址: http://phantomjs.org/ ,包括 Windows ,Mac OS,linux版本,自行选择对应 版本下载解压即可( 为方便使用,可自已为phantomjs设置环境变量 ),其中带有一个example文件夹,里面有很多已经写好的代码供使用。本文假设phantomjs已经安装好并已设置了环境变量。
神马是中间件?
# -*- coding: utf-8 -*-from selenium import webdriverfrom scrapy.conf import settings# from scrapy.http.response import Responsefrom scrapy.http import HtmlResponseimport timefrom scrapy import signalsfrom scrapy.xlib.pydispatch import dispatcher from telnetlib import DOclass JavaScriptMiddleware(object): def __init__(self): if settings['LOGIN_TYPE'] == 'MyCrawl': ''' self.simulation = weibo_login(settings['USERNAME'], settings['PWD'], settings['COOKIE_FILE']) cookie_file = settings['COOKIE_FILE'] cookie_jar = cookielib.LWPCookieJar(cookie_file) cookie_jar.load(ignore_discard=True, ignore_expires=True) self.driver = webdriver.PhantomJS(executable_path=settings['JS_BIN']) for c in cookie_jar: self.driver.add_cookie({'name': c.name, 'value': c.value, 'path': '/', 'domain': c.domain}) ''' # simulate user login PRocess self.driver = webdriver.PhantomJS(executable_path=settings['JS_BIN'])# 登录# self.driver.get('http://login.sina.com.cn/')# uid = self.driver.find_element_by_id('username')# upw = self.driver.find_element_by_id('passWord')# loginBtn = self.driver.find_element_by_class_name('smb_btn')# time.sleep(1)# uid.send_keys(settings['USERNAME'])# upw.send_keys(settings['PWD'])# loginBtn.click()# time.sleep(1) elif settings['LOGIN_TYPE'] == 'other': print('add login code') pass else: self.driver = webdriver.PhantomJS(executable_path=settings['JS_BIN']) dispatcher.connect(self.spider_closed, signals.spider_closed) def process_request(self, request, spider): self.driver.get(request.url) print("页面渲染中····开始自动下拉页面") indexPage = 1000 while indexPage<self.driver.execute_script("return document.body.offsetHeight"): self.driver.execute_script("scroll(0,"+str(indexPage)+")") indexPage = indexPage +1000 print(indexPage) time.sleep(1) rendered_body = self.driver.page_source #编码处理 if r'charset="GBK"' in rendered_body or r'charset=gbk' in rendered_body: coding = 'gbk' else: coding = 'utf-8' return HtmlResponse(request.url, body=rendered_body, encoding='utf-8') #关闭浏览器 def spider_closed(self, spider, reason): print ('close driver......') self.driver.close()新闻热点
疑难解答