对于动态数据的爬取,可以选择selenium和PhantomJS两种方式,本文选择的是PhantomJS。
网址:
https://s.taobao.com/search?q=%E7%AC%94%E8%AE%B0%E6%9C%AC%E7%94%B5%E8%84%91&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306
1.首先第一步,对中间件的设置。
进入pipelines.py文件中:
from selenium import webdriver
from scrapy.http.response.html import HtmlResponse
from scrapy.http.response import Response
class SeleniumSpiderMiddleware(object):
def __init__(self):
self.driver = webdriver.PhantomJS()
def process_request(self ,request ,spider):
# 当引擎从调度器中取出request进行请求发送下载器之前
# 会先执行当前的爬虫中间件 ,在中间件里面使用selenium
# 请求这个request ,拿到动态网站的数据 然后将请求
# 返回给spider爬虫对象
if spider.nam