初识Scrapy

作者: 违心唯心 | 来源:发表于2019-10-23 23:16 被阅读0次

一.基本命令

创建项目 scrapy startproject 项目名字
创建爬虫文件 scrapy genspider 文件名 域名
运行爬虫项目 scrapy crawl name属性
查看可运行的爬虫 scrapy list

二.小试牛刀(scrapy.Spider)

全书网

  1. 创建项目
scrapy startproject qsw_spider

2.创建爬虫文件

cd  qsw_spider
scrapy genspider qsw_test www.quanshuwang.com

3.设置字段,,,,,items.py


spider02.png
import scrapy
class QswSpiderItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    title = scrapy.Field()
    author = scrapy.Field()
    context = scrapy.Field()
    link_url = scrapy.Field()

4.编写爬虫文件,,,qsw_test.py


spider01.png
# -*- coding: utf-8 -*-
import scrapy
from ..items import QswSpiderItem

class QswTestSpider(scrapy.Spider):
    name = 'qsw_test'
    # allowed_domains = ['www.quanshuwang.com']
    start_urls = ['http://www.quanshuwang.com/list/1_1.html']
    def parse(self, response):
        '''
        解析网页
        :param response:
        :return:
        '''
        link_url_list = response.xpath(r'//ul[@class="seeWell cf"]/li/a/@href').extract()
        title_list = response.xpath(r'//ul[@class="seeWell cf"]/li/span/a[@target="_blank"]/text()').extract()
        author_list = response.xpath(r'//ul[@class="seeWell cf"]/li/span/a[2]/text()').extract()
        context_list = response.xpath(r'//ul[@class="seeWell cf"]/li/span/em/text()').extract()

        for title,author,context,link_url in zip(title_list,author_list,context_list,link_url_list):
            items = QswSpiderItem()
            items['title']= title
            items['author']= author
            items['context']= context
            items['link_url']= link_url
            yield items

        next_url = response.xpath('//div[@class="pages"]/div[@class="pagelink"]/a[@class="next"]/@href').extract()
        if next_url:
            yield scrapy.Request(next_url[0], callback=self.parse)

5.设置管道,,,pipelines.py


spider03.png
class QswSpidertestPipeline(object):
    def __init__(self):
        self.fa = open('result.json','w+',encoding='utf-8')
    def process_item(self, item, spider):
        self.fa.write(str(item)+'\r\n')
        return item
    def close_spider(self,spider):
        self.fa.close()
  1. 激活 管道,,,,settings.py
ITEM_PIPELINES = {
   'qsw_spider.pipelines.QswSpidertestPipeline': 300,
}
  1. 运行爬虫
scrapy crawl  qsw_test

三.另一个爬虫(CrawlSpider)

简书,需要设置UA,打开settings.py

DEFAULT_REQUEST_HEADERS = {
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'Accept-Language': 'en',
   'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0',
}
  1. 创建文件


    spider01.png
cd/d D:\Python_scrapy_test\spider_qsw\qsw_spider>
#创建CrawlSpider爬虫文件
scrapy genspider -t crawl js jianshu.com

2.设置需要的字段 items.py


spider04.png
class JianshuItem(scrapy.Item):
    title = scrapy.Field()
    url = scrapy.Field()
    author = scrapy.Field()
    pub_time = scrapy.Field()
    read_count = scrapy.Field()
    word_count = scrapy.Field()
  1. 编写爬虫文件 js.py


    spider02.png
spider03.png
# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule


class JsSpider(CrawlSpider):
    name = 'js'
    # allowed_domains = ['jianshu.com']
    start_urls = ['https://www.jianshu.com/']

    rules = (
        Rule(LinkExtractor(allow=r'https://www.jianshu.com/p/.*?'), callback='parse_item', follow=True),  #简书后面的是uid,匹配uid即可
    )

    def parse_item(self, response):
        item = {}
        #item['domain_id'] = response.xpath('//input[@id="sid"]/@value').get()
        #item['name'] = response.xpath('//div[@id="name"]').get()
        #item['description'] = response.xpath('//div[@id="description"]').get()
        item['title'] = response.xpath(r'//h1[@class="_1RuRku"]/text()').extract_first()
        item['author'] = response.xpath(r'//span[@class="_22gUMi"]/text()').extract_first()
        item['pub_time'] = response.xpath(r'//time/text()').extract_first()
        item['url'] = response.url
        count = response.xpath(r'//div[@class="s-dsoj"]/span[2]/text()').extract_first()
        if '字数' in count:
            item['word_count'] = count.split(' ')[-1]
            item['read_count'] = response.xpath(r'//div[@class="s-dsoj"]/span[3]/text()').extract_first().split(' ')[-1]
        elif '阅读' in count:
            item['read_count'] = count.split(' ')[-1]
            item['word_count'] = response.xpath(r'//div[@class="s-dsoj"]/span[1]/text()').extract_first().split(' ')[-1]
        yield item
  1. 设置管道,,,pipelines.py


    spider05.png
class JianShuPipeline(object):
    def __init__(self):
        self.f = open('result_jianshu.json','w+',encoding='utf-8')
    def process_item(self, item, spider):
        self.f.write(str(item)+'\r\n')
        return item
    def close_spider(self,spider):
        self.f.close()

5.激活简书管道 settings.py


spider06.png
ITEM_PIPELINES = {
   'qsw_spider.pipelines.JianShuPipeline': 301,
}
  1. 运行爬虫
scrapy crawl  js
  1. 效果图


    spider07.png

相关文章

  • Scrapy初识

    创建Scrapy项目 首先打开命令行,进入你想存放scrapy项目的目录下,输入以下命令 将会创建firstscr...

  • 初识Scrapy

    为什么使用Scrapy? 我们可以用requests和beautifulsoup完成一个实用的爬虫,但如果想大规模...

  • 初识Scrapy

    一.基本命令 创建项目 scrapy startproject 项目名字创建爬虫文件 scrapy genspi...

  • 初识scrapy

    学习scrapy笔记链接:https://pan.baidu.com/s/1NLpLEk2cv8QbssFV-Pz...

  • Scrapy爬取数据初识

    Scrapy爬取数据初识 初窥Scrapy Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架。 ...

  • 初识Scrapy框架

    Scrapy是一个流行的网络爬虫框架。 Ubuntu下安装 sudo apt-get install python...

  • 1、初识scrapy

    Scrapy是一个从网上爬取数据的开源的、友好的框架。 An open source and collaborat...

  • Scrapy定时爬虫总结&Docker/K8s部署

    初识Scrapy Scrapy是Python开发的一个快速、高层次的屏幕抓取和web抓取框架,用于抓取web站点并...

  • Scrapy 入门学习 1 & 初识Scrapy

    引子 最近工作上需要对Scrapy进行二次开发,为此我又好好的复习了一下Scrapy相关的知识,并整理了如下内容 ...

  • scrapy框架是真爱

    初识scrapy框架 首先我认为scrapy框架和编写的普通爬虫文件没有什么区别 唯一不同的是它可以把你得各种爬虫...

网友评论

    本文标题:初识Scrapy

    本文链接:https://www.haomeiwen.com/subject/eywkvctx.html