一. Scrapy简介及安装

http://python.jobbole.com/86405/ Scrapy的详细介绍
 
1.简介
 
2.安装
    1.window上安装:
        先安装依赖包:pip3 install wheel
                    https://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted 下载以后安装pip3 install 安装包 
                    pip3 install pywin32    
                    pip3 install scrapy
    2mac上: pip3 install scrapy
 
 

二. Scrapy常见命令

 
1.创建项目:      scrapy startproject project-name
2.创建爬虫文件:   scrapy genspider  filename  指定网站
3.运行项目:       scrapy crawl filename 
                 scrapy crawl filename --nolog  不打印日志
 
 

三.Scrapy的基本使用

1.创建项目.
        通过命令进行创建: scrapy startproject   项目名
2.自动创建目录的结果.
          
    
    文件说明:
        1.boss.py:爬虫文件.一般创建爬虫文件时,以网站域名命名.
                     通过命令创建: scrapy  genspider  boss  指定网站
        2. items.py:设置数据储存模块,用于结构化数据
        3. middlewares
        4.pipelines
        5.settings:配置文件.
        6.spiders   爬虫目录.如:创建文件编写爬虫规则
3,编写文件
(1)爬虫文件中
# -*- coding: utf-8 -*-
import scrapy
from bossDemo.items import BossdemoItem
# 爬虫文件的作用
# 1.url的指定
# 2.请求的发送
# 3.数据的解析
# 4.将item对象通过yield传给管道文件
class BossSpider(scrapy.Spider):
    name = 'boss'
    # allowed_domains = ['www.xxx.com']
    start_urls = ['https://www.zhipin.com/job_detail/?query=python&scity=101180100&industry=&position=']
 
    def parse(self, response):
        li_list = response.xpath('//*[@id="main"]/div/div[3]/ul/li')
        # print(li_list)
        for li in li_list:
            title = li.xpath('./div/div[1]/h3/a/div[1]/text()').extract_first()
            salary = li.xpath('./div/div[1]/h3/a/span/text()').extract_first()
            company = li.xpath('./div/div[2]/div/h3/a/text()').extract_first()
            print(title +salary + company)
            # 实例化一个item对象
            item = BossdemoItem()
            # 将解析后的数据储存到item对象中
            item['title'] = title
            item['salary'] = salary
            item['company'] = company
            # 将item对象传给管道文件进行持久化储存
            yield item
(2)items.py
import scrapy
 
 
class BossdemoItem(scrapy.Item):
    # define the fields for your item here like:
    title = scrapy.Field()
    salary = scrapy.Field()
    company = scrapy.Field()
(3)Pipelines.py
import pymysql
from redis import Redis
import json
 
class BossdemoPipeline(object):
    # 这个函数只会在开始爬取的时候执行一次
    def open_spider(self, spider):
        print('爬虫开始')
        self.fp = open('./job.txt', 'w', encoding='utf-8' )
 
    # 每提交一次item,这个文件就执行一次
    def process_item(self, item, spider):
        self.fp.write(item['title'] + '\t' + item['salary'] + '\t' + item['company'] + '\n')
        print('爬取中')
        return item
 
    # 存储结束后执行这个函数
    def close_spider(self,spider):
        print('爬虫结束')
        self.fp.close()
 
 
class MysqlPipeline(object):
    cursor = None
    conn = None
 
    def open_spider(self, spider):
        print('mysql爬虫开始')
        self.conn = pymysql.Connect(host='127.0.0.1', port=3306, user='root', password='', db='db1' )
 
    def process_item(self, item, spider):
        self.cursor = self.conn.cursor()
        sql = 'insert into boss (title, salary, company) values ("%s","%s","%s")'%(item["title"], item["salary"],item["company"])
        try:
            print('mysql爬虫中')
            self.cursor.execute(sql)
self.conn.commit()
 
        except  Exception as e:
            print(e)
            self.conn.rollback()
        return item
    def close_spider(self, spider):
        print('mysql爬虫结束')
        self.cursor.close()
        self.conn.close()
 
 
class RedisPipeline(object):
 
    def open_spider(self, spider):
        print('redis储存')
        self.conn = Redis(host='127.0.0.1', port='6379')
 
    def process_item(self, item, spider):
        dic = {
            'title': item['title'],
            'salary': item['salary'],
            'company': item['company'],
        }
        print('redis存储中...')
        self.conn.lpush('Jobinfo', json.dumps(dic))
 
    def close_spider(self, spider):
        print('redis结束')
 
 
备注:
        1.爬虫文件需要定义一个类,并继承(scrapy.Spider)
        2.必须定义name, 即爬虫名,如果没有那么,会报错,因为源码中这样规定的:
 
        3.编写函数parse,这里需要注意的是,该函数不能改变,是因为Scrapy中默认callback函数的函数名就是parse.
 4.scrapy发送post请求
# 1.scrapy中post请求的发送:重写源码中的start_requests方法
#  因为源码中这样写的:for url in self.start_urls:
#                           yield self.make_requests_from_url(url)   (make_requests_from_url方法的返回结果是Request对象)
# 2.在scrapy 框架中,会自动对cookie进行处理,可以在settings中设置不处理 COOKIES_ENABLED = False
 
示例代码:
import scrapy
class LoginSpider(scrapy.Spider):
    name = 'login'
    # allowed_domains = ['www.xxx.com']
    start_urls = ['http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=201903160368']
 
    def start_requests(self):
        data = {
            "email": "15516092050",
            "icode": '',
            "origURL": "http://www.renren.com/home",
            "domain": "renren.com",
            "key_id": '1',
            "captcha_type": "web_login",
            "password": "5e088a2ee22d34dd081aac25578e67bd3a2d851cdfbcf1f0c9ab7056bd1bad62",
            "rkey": "3f4696f6fa1b89e9061868300bf11484",
            "f": "http%3A%2F%2Fwww.renren.com%2F969395731",
        }
        for url in self.start_urls:
            yield scrapy.FormRequest(url=url, formdata=data, callback=self.parse)
 
    def parse(self, response):
        detail_url = 'http://www.renren.com/969395731'
        yield scrapy.Request(url = detail_url, callback=self.GetDetail)
 
    def GetDetail(self, response):
        ret = response.text
        print(ret)
5.scrapy请求传参的方式
1.用scrapy爬取数据时,如果发现需要爬取的数据不在同一页面内,则必须使用请求传参的方式进行持久化储存
2.示例代码
class MovieSpider(scrapy.Spider):
    name = 'movie'
    # allowed_domains = ['www.xxx.com']
    start_urls = ['http://www.55xia.com/']
 
    def parse(self, response):
        div_list = response.xpath('/html/body/div[1]/div[2]/div[1]/div/div | /html/body/div[1]/div[2]/div[3]/div/div')
        for div in div_list:
            item = MoviedemoItem()
            detail_url = div.xpath('./div/div/h1/a/@href')
            if not detail_url:
                continue
            else:
                detail_url = 'http:' + detail_url.extract_first()
            name = div.xpath('./div/div/h1/a/text()').extract_first()
            score = div.xpath('./div/div/h1/em/text()')
            if not score:
                score = '暂无评价'
            else:
                score = score.extract_first()
            item['name'] = name
            item['score'] = score
            print(name)
            print(score)
            yield scrapy.Request(url=detail_url, callback=self.GetDetail, meta={'item':item})
 
    def GetDetail(self,response):
        item = response.meta['item']
        direct = response.xpath('/html/body/div[1]/div/div/div[1]/div[1]/div[2]/table/tbody/tr[1]/td[2]/a/text()').\
            extract_first()
        detail = response.xpath('/html/body/div[1]/div/div/div[1]/div[2]/div[2]//text()'). extract_first()
        item['direct'] = direct
        item['detail'] = detail
        print(direct)
        print(detail)
        yield item
6.提高scrapy框架效率
# 增加并发:
#     默认scrapy开启的并发线程为32个,可以适当进行增加。在settings配置文件中修改CONCURRENT_REQUESTS = 100值为100,并发设置成了为100。
#
# 降低日志级别:
#     在运行scrapy时,会有大量日志信息的输出,为了减少CPU的使用率。可以设置log输出信息为INFO或者ERROR即可。在配置文件中编写:LOG_LEVEL = ‘INFO’
#
# 禁止cookie:
#     如果不是真的需要cookie,则在scrapy爬取数据时可以禁止cookie从而减少CPU的使用率,提升爬取效率。在配置文件中编写:COOKIES_ENABLED = False
#
# 禁止重试:
#     对失败的HTTP进行重新请求(重试)会减慢爬取速度,因此可以禁止重试。在配置文件中编写:RETRY_ENABLED = False
#
# 减少下载超时:
#     如果对一个非常慢的链接进行爬取,减少下载超时可以能让卡住的链接快速被放弃,从而提升效率。在配置文件中进行编写:DOWNLOAD_TIMEOUT = 10 超时时间为10s
 
CONCURRENT_REQUESTS = 10
LOG_LEVEL = 'ERROR'
COOKIES_ENABLED = False
RETRY_ENABLED = False
DOWNLOAD_TIMEOUT = 5
7.设置代理池和UA池
1.UA池:User-Agent池
- 作用:尽可能多的将scrapy工程中的请求伪装成不同类型的浏览器身份。
- 操作流程:
    1.在下载中间件中拦截请求
    2.将拦截到的请求的请求头信息中的UA进行篡改伪装
    3.在配置文件中开启下载中间件
user_agent_list = [
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 "
        "(KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1",
        "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 "
        "(KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 "
        "(KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 "
        "(KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",
        "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 "
        "(KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 "
        "(KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5",
        "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 "
        "(KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 "
        "(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 "
        "(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 "
        "(KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 "
        "(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 "
        "(KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 "
        "(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 "
        "(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 "
        "(KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 "
        "(KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3",
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 "
        "(KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",
        "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 "
        "(KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24"
]
 
2.代理池
- 作用:尽可能多的将scrapy工程中的请求的IP设置成不同的。
- 操作流程:
    1.在下载中间件中拦截请求
    2.将拦截到的请求的IP修改成某一代理IP
    3.在配置文件中开启下载中间件
示例代码
http_proxy = ['http://91.226.35.93:53281', 'http://110.52.235.73:9999', 'http://151.3.53.246:53281']
https_proxy = ['https://106.104.168.15:8080', 'https://93.190.143.59:1080', 'https://223.27.212.41:8080' ]
if request.url.split(':')[0] == 'http':
    request.meta['proxy'] = random.choice(http_proxy)
else:
    request.meta['proxy'] = random.choice(https_proxy)
8 五大组件
 
 

爬虫之牛掰的scrapy框架的更多相关文章

  1. python爬虫---js加密和混淆,scrapy框架的使用.

    python爬虫---js加密和混淆,scrapy框架的使用. 一丶js加密和js混淆 js加密 ​ 对js源码进行加密,从而保护js代码不被黑客窃取.(一般加密和解密的方法都在前端) http:// ...

  2. 网络爬虫第五章之Scrapy框架

    第一节:Scrapy框架架构 Scrapy框架介绍 写一个爬虫,需要做很多的事情.比如:发送网络请求.数据解析.数据存储.反反爬虫机制(更换ip代理.设置请求头等).异步请求等.这些工作如果每次都要自 ...

  3. python爬虫入门(七)Scrapy框架之Spider类

    Spider类 Spider类定义了如何爬取某个(或某些)网站.包括了爬取的动作(例如:是否跟进链接)以及如何从网页的内容中提取结构化数据(爬取item). 换句话说,Spider就是您定义爬取的动作 ...

  4. 爬虫入门(四)——Scrapy框架入门:使用Scrapy框架爬取全书网小说数据

    为了入门scrapy框架,昨天写了一个爬取静态小说网站的小程序 下面我们尝试爬取全书网中网游动漫类小说的书籍信息. 一.准备阶段 明确一下爬虫页面分析的思路: 对于书籍列表页:我们需要知道打开单本书籍 ...

  5. Python3爬虫(十八) Scrapy框架(二)

    对Scrapy框架(一)的补充 Infi-chu: http://www.cnblogs.com/Infi-chu/ Scrapy优点:    提供了内置的 HTTP 缓存 ,以加速本地开发 .   ...

  6. python网络爬虫(1)——安装scrapy框架的常见问题及其解决方法

    Scrapy是为了爬取网站数据而编写的一款应用框架,出名,强大.所谓的框架其实就是一个集成了相应的功能且具有很强通用性的项目模板. 其实在Linux和 Mac安装,就简单的pip命令即可: pip i ...

  7. Python之爬虫(十四) Scrapy框架的架构和原理

    这一篇文章主要是为了对scrapy框架的工作流程以及各个组件功能的介绍 Scrapy目前已经可以很好的在python3上运行Scrapy使用了Twisted作为框架,Twisted有些特殊的地方是它是 ...

  8. Python之爬虫(十五) Scrapy框架的命令行详解

    这篇文章主要是对的scrapy命令行使用的一个介绍 创建爬虫项目 scrapy startproject 项目名例子如下: localhost:spider zhaofan$ scrapy start ...

  9. python爬虫入门(八)Scrapy框架之CrawlSpider类

    CrawlSpider类 通过下面的命令可以快速创建 CrawlSpider模板 的代码: scrapy genspider -t crawl tencent tencent.com CrawSpid ...

随机推荐

  1. vuex 子组件传值

    以下是基础的使用方法,详细且深入使用方法详细见博客:https://segmentfault.com/a/1190000015782272 Vuex官网地址:https://vuex.vuejs.or ...

  2. vue【指令】

    <div class="m-conbox"> <div v-text="html"></div> <div>{{ ...

  3. 在Linux直接运行安卓程序

    Linux上的软件少得可怜,要是能够直接运行安卓程序,那将是意见很酷的事情. 方法原理:首先这个方法不需要开启安卓虚拟机,是直接在Linux上运行的. 谷歌在很早之前提出了archon的方案,能够直接 ...

  4. Linux ssh将命令放入后台

    如何在关闭ssh连接的情况下,让程序继续运行? 对Unix,Linux类服务器维护经常是通过ssh完成的,而有些操作比较费时,如更新程序等.此时如果断开ssh连接的话,更新程序就会随之被中断.如何保证 ...

  5. Py中map与np.rival学习

    转自:廖雪峰网站 1.map/reduce map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. 举例说明 ...

  6. SQL _ Create Procedure

    -- ================================================ -- Template generated from Template Explorer usi ...

  7. 安装程序无法打开注册表项 UNKNOWN\Components\…的简单解决办法(转)

    安装程序无法打开注册表项 UNKNOWN\Components\…的简单解决办法 2018年04月16日 16:41:32 super_star_贤 阅读数:7193   在安装软件时(比如安装SQL ...

  8. Oracle使用rman备份数据库时出现cannot reclaim的错误

    1. 按照<2 day DBA>中的guide,设置fast recovery area. SQL> ALTER SYSTEM SET DB_RECOVERY_FILE_DEST_S ...

  9. GameObject.Find与Transform.Find的区别

    1.GameObject.Find 函数原型: public static GameObject Find(string name); 说明:1.GameObject只能查找到active的物体 2. ...

  10. RepRap Prusa i3 平台自動補正

    RepRap Prusa i3 平台自動補正 平台校正不但費時,而且經常失敗,時在是很令人洩氣!期盼了好一陣子,Marlin終於將平台自動補正的功能加進來了!!這個功能將原本Z軸的Endstop,改裝 ...