美文网首页
爬虫scrapy框架(2)

爬虫scrapy框架(2)

作者: 猛犸象和剑齿虎 | 来源:发表于2019-05-27 06:39 被阅读0次
t013b9c86f5a43c0037.jpg

框架内的所有文件都不要删除。

  • 我们把项目文件放入编辑器中,小道用的pycharm.


    image.png

框架入门

  • 目标,爬取好听轻音乐网的歌曲名和艺术家。http://www.htqyy.com/top/hot
  • 首先定义目标数据字段到items文件中。
# -*- 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 MyspiderItem(scrapy.Item):
    # define the fields for your item here like:
    title = scrapy.Field()#歌曲名
    artist= scrapy.Field()#艺术家
# -*- coding: utf-8 -*-
import scrapy


class MusicspiderSpider(scrapy.Spider):
    name = 'musicspider'#爬虫识别名称
    allowed_domains = ['htqyy.com']#爬虫能够爬取的网址范围
    start_urls = ['http://www.htqyy.com/top/musicList/hot?pageIndex=0&pageSize=20']#爬取的起始url

    def parse(self, response):
        filename='music.html'
        data= response.body#获取响应内容
        open(filename,'wb').write(data)#写入本地,请求的动作被框架完成

在这个文件夹中打开命令窗口


image.png
输入:scrapy crawl musicspider image.png
  • html信息出现。 image.png

相关文章

网友评论

      本文标题:爬虫scrapy框架(2)

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