scrapy爬取二级页面的内容

1.定义数据结构item.py文件

# -*- coding: utf-8 -*-
'''
field: item.py
'''
# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html import scrapy class TupianprojectItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
# 图片标题
title = scrapy.Field()
# 发布时间
publish_time = scrapy.Field()
# 浏览量
look = scrapy.Field()
# 收藏量
collect = scrapy.Field()
# 下载量
download = scrapy.Field()
# 图片链接
image_url = scrapy.Field()

2.爬虫文件

# -*- coding: utf-8 -*-
import scrapy from tupianproject.items import TupianprojectItem class ImageSpider(scrapy.Spider):
name = 'image'
allowed_domains = ['699pic.com']
start_urls = ['http://699pic.com/people-1-0-0-0-0-0-0.html'] url = 'http://699pic.com/people-{}-0-0-0-0-0-0.html'
page = 1 def parse(self, response):
# 在一级页面中,应该将所有的图片详情页面的链接获取到
image_detail_url_list = response.xpath('//div[@class="list"]/a/@href').extract()
# pass
# 遍历详情页面,向每一个详情页面发送请求即可
for image_detail_url in image_detail_url_list:
yield scrapy.Request(url=image_detail_url, callback=self.parse_detail) # 接着发送其他请求
if self.page <= 3:
self.page += 1
url = self.url.format(self.page)
yield scrapy.Request(url=url, callback=self.parse) def parse_detail(self, response):
# 创建一个item对象
item = TupianprojectItem()
# 提取图片的每一个信息
# title
item['title'] = response.xpath('//div[@class="photo-view"]/h1/text()').extract_first()
# 发布时间
item['publish_time'] = response.xpath('//div[@class="photo-view"]/div/span[@class="publicityt"]')[0].xpath('string(.)').extract_first()
# 获取浏览量
item['look'] = response.xpath('//div[@class="photo-view"]/div/span[@class="look"]/read/text()').extract_first()
# 获取收藏量
item['collect'] = response.xpath('//div[@class="photo-view"]/div/span[@class="collect"]')[0].xpath('string(.)').extract_first()
# 获取下载量
item['download'] = response.xpath('//div[@class="photo-view"]/div/span[@class="download"]')[0].xpath('string(.)').extract_first().strip('\n\t')
# 获取图片的链接
item['image_url'] = response.xpath('//div[@class="huabu"]//img/@src').extract_first()
# 将item发送出去
yield item

3.管道文件

# -*- coding: utf-8 -*-
'''
filed: pipelines.py
'''
s
# 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 import json
import urllib.request
import os class TupianprojectPipeline(object):
def open_spider(self, spider):
self.fp = open('tupian.json', 'w', encoding='utf8') def process_item(self, item, spider):
d = dict(item)
string = json.dumps(d, ensure_ascii=False)
self.fp.write(string + '\n') # 下载图片
self.download(item)
return item def download(self, item):
dirname = './people'
suffix = item['image_url'].split('.')[-1]
filename = item['title'] + '.' + suffix
filepath = os.path.join(dirname, filename)
urllib.request.urlretrieve(item['image_url'], filepath) def close_spider(self, spider):
self.fp.close()

scrapy(四): 爬取二级页面的内容的更多相关文章

  1. scrapy框架爬取多级页面

    spides.py # -*- coding: utf-8 -*- import scrapy from weather.items import WeatherItem from scrapy.cr ...

  2. [scrapy]实例:爬取jobbole页面

    工程概览: 创建工程 scrapy startproject ArticleSpider 创建spider cd /ArticleSpider/spiders/ 新建jobbole.py # -*- ...

  3. Scrapy 框架 使用 selenium 爬取动态加载内容

    使用 selenium 爬取动态加载内容 开启中间件 DOWNLOADER_MIDDLEWARES = { 'wangyiPro.middlewares.WangyiproDownloaderMidd ...

  4. Scrapy爬取静态页面

    Scrapy爬取静态页面 安装Scrapy框架: Scrapy是python下一个非常有用的一个爬虫框架 Pycharm下: 搜索Scrapy库添加进项目即可 终端下: #python2 sudo p ...

  5. scrapy模拟浏览器爬取验证码页面

    使用selenium模块爬取验证码页面,selenium模块需要另外安装这里不讲环境的配置,我有一篇博客有专门讲ubuntn下安装和配置模拟浏览器的开发 spider的代码 # -*- coding: ...

  6. 【Scrapy(四)】scrapy 分页爬取以及xapth使用小技巧

    scrapy 分页爬取以及xapth使用小技巧 这里以爬取www.javaquan.com为例: 1.构建出下一页的url: 很显然通过dom树,可以发现下一页所在的a标签   2.使用scrapy的 ...

  7. Scrapy+selenium爬取简书全站

    Scrapy+selenium爬取简书全站 环境 Ubuntu 18.04 Python 3.8 Scrapy 2.1 爬取内容 文字标题 作者 作者头像 发布日期 内容 文章连接 文章ID 思路 分 ...

  8. 使用scrapy爬虫,爬取17k小说网的案例-方法二

    楼主准备爬取此页面的小说,此页面一共有125章 我们点击进去第一章和第一百二十五章发现了一个规律 我们看到此链接的  http://www.17k.com/chapter/271047/6336386 ...

  9. 简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息

    简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息 简单的scrapy实战:爬取腾讯招聘北京地区的相关招聘信息 系统环境:Fedora22(昨天已安装scrapy环境) 爬取的开始URL:ht ...

随机推荐

  1. 小孩学习编程的绝佳游戏——CodeMonkey

    CodeMonkey于2014年1月在以色列成立.它的愿景是建立一个全球性的学习平台,让孩子们通过游戏的方式学习.发现.创造和分享,同时在此过程中获得编程这一项21世纪必备的技能. 通常提到CodeM ...

  2. Dart Memo for Android Developers

    Dart Memo for Android Developers Dart语言一些语法特点和编程规范. 本文适合: 日常使用Kotlin, 突然想写个Flutter程序的Android程序员. Dar ...

  3. ViewDragHelper类的基本使用

    在android的开发包android.support.v4.widget中有一个ViewDragHelper类.这个类的作用是帮助我们处理View的拖拽滑动.在一个ViewGroup类的内部定义一个 ...

  4. post请求头的常见类型

    1.application/json(JSON数据格式) xhr.setRequestHeader("Content-type","application/json; c ...

  5. 手把手教你利用Docker+jenkins部署你的网站

    更新服务器的安装源为阿里的源,参考链接:https://blog.csdn.net/js_xh/article/details/79166655 安装docker; 1 更新资源 sudo apt-g ...

  6. 阿里云Ubuntu配置mysql+navicat连接

    一>mysql安装配置(工具:Xshell6) ​ 1.安装mysql apt-get install mysql-server mysql-client ​ 2.查看安装:查看版本 sudo ...

  7. Centos7下的MySQL5.6安装

    yum install wget yum install perl perl-devel cd /usr/local/src wget https://cdn.mysql.com//Downloads ...

  8. Selenium Grid的原理、配置与使用(转)

    Selenium GridSelenium Grid在前面介绍Selenium的时候说过它有三大组件,Selenium Grid就是其中之一而作用就是分布式执行测试.讲分布式之前还是要说说UI自动化的 ...

  9. xxl-job搭建、部署、SpringBoot集成xxl-job

    一.搭建xxl-job 1.下载xxl-job代码 码云地址:https://gitee.com/xuxueli0323/xxl-job gitHub地址:https://github.com/xux ...

  10. Ubuntu18.04 IP配置问题

    18.04 LTS 提供了通过 netplan.io 轻松配置网络连接 参考 Ubuntu18.04 发行release cn.ubuntu.com/server