scrapy爬虫完整的代码实例[通俗易懂]

scrapy爬虫完整的代码实例[通俗易懂]新建工程scrapystartprojecttutorial进入tutorial目录,在spider下面新建quotes_spider.pyimportscrapyfrom..itemsimportQuotesItem#coding:utf-8classQuotesSpider(scrapy.Spider):name=”quot…

大家好,又见面了,我是你们的朋友全栈君。

    新建工程

scrapy startproject tutorial

    进入tutorial目录,在spider下面新建quotes_spider.py

import scrapy
from ..items import QuotesItem

#coding:utf-8

class QuotesSpider(scrapy.Spider):
    name = "quotes"
    allowed_domain = "toscrape.com"

    def start_requests(self):
        for i in range(1,2):
            url = "http://quotes.toscrape.com/page/" + str(i) + "/"
            yield scrapy.Request(url=url,callback=self.parse)


    def parse(self, response):
        item = QuotesItem()
        for quote in response.css('div.quote'):
            item['text'] = quote.css('span.text::text').get(),
            item['author'] = quote.css('small.author::text').get(),
            item['tags'] = quote.css('div.tags a.tag::text').getall()
            yield item

  进入items.py,代码如下:

# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy


class TutorialItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    pass

class QuotesItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    text = scrapy.Field()
    author = scrapy.Field()
    tags = scrapy.Field()
    pass

    进入pipelines.py进行设置,对数据进行清洗

# -*- coding: utf-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html


class TutorialPipeline(object):
    def process_item(self, item, spider):
        return item

class QuotesPipeline(object):
    def process_item(self,item, spider):
        if item['text']:
            item['text'] = item['text'][0][1:-1]
        if item['author']:
            item['author'] = item['author'][0][1:-1]
        return item

    进入setting.py,修改相关配置

ITEM_PIPELINES = {
   'tutorial.pipelines.TutorialPipeline': 300,
   'tutorial.pipelines.QuotesPipeline': 500,
}
FEED_EXPORT_ENCODING = 'utf-8'

    进行命令行,执行爬虫

scrapy crawl quotes -o quotes.jl
import json
import codecs

class JsonPipeline(object):
    def __init__(self):
        self.file = codecs.open('logs.json', 'w', encoding='utf-8')
    def process_item(self, item, spider):
        line = json.dumps(dict(item), ensure_ascii=False) + "\n"
        self.file.write(line)
        return item
    def spider_closed(self, spider):
        self.file.close()

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/153159.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 第一次训练赛总结

    第一次训练赛总结

    2021年9月27日
    48
  • linux vim常用命令_linuxvi编辑器命令

    linux vim常用命令_linuxvi编辑器命令1.vi模式a)一般模式:vi处理文件时,一进入该文件,就是一般模式了.b)编辑模式:在一般模式下可以进行删除,复制,粘贴等操作,却无法进行编辑操作。等按下‘i,I,o,O,a,A,r,R’等字母之后才能进入编辑模式.通常在linux中,按下上述字母时,左下方会出现’INSERT’或者‘REPLACE’字样,才可以输入任何文字到文件中.要回到一般模式,按下[ESC]键即可.c)命令行模…

    2022年9月22日
    2
  • 使用 Android Studio 搭建安卓开发环境[通俗易懂]

    使用 Android Studio 搭建安卓开发环境[通俗易懂]使用AndroidStudio搭建安卓开发环境,方便、快捷。因为AndroidSDK等下载已经集成到AndroidStudio的安装中1、官网下载AndroidStudio编辑器首先,访问谷歌中国开发者网站下载AndroidStudio编辑器:https://developer.android.google.cn/studio选择要下…

    2022年4月18日
    465
  • VMware Ubuntu安装详细过程(详细图解)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家!一.下载Ubuntu镜像文件下载地址:http://mirrors.aliyun.com/ubuntu-releases/16.04/进入下载页面,如下图选择版本点击即可下载二.下载及安装VMware下载地址:https://pan.baidu.com/s/1aEEI-DRa4oKeViddxW2CPA…

    2022年4月7日
    70
  • IOSG Ventures宣布加入Celer状态守卫者网络以及cBridge流动性桥接网络

    IOSG Ventures宣布加入Celer状态守卫者网络以及cBridge流动性桥接网络IOSGVentures宣布加入Celer状态守卫者网络,并已建立Celer验证人节点。同时,IOSGVentures也宣布加入CelercBridge流动性桥接网络,为其提供流动性。IOSGVentures将与Celer共同维护其二层扩容网络的可用性和安全性,并为其生态发展提供持续可靠的基础设施服务。IOSGVentures现已成为Celer状态守卫者网络验证人节点IOSGVentures成立于2017年,是由社区驱动的研究型早期美元基金,在中国、美国和新…

    2022年6月4日
    35
  • 列车调度C语言数据结构,数据结构——列车调度

    列车调度C语言数据结构,数据结构——列车调度题目链接:https://pintia.cn/problem-sets/1045870129681440768/problems/1045870197130047495#p-2题目大意:给你一列火车,上面有表号,问给你几个火车隧道,能使车厢从大到小。一道有思维结构的模拟题。先说一下核心解体思想:就是一个序列里,有多少个从大到小排好序的序列,求个数。朴素的模拟思想,先读入一个数组,从头到尾判断,含有…

    2022年7月26日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号