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. ping外网:unknown host www.baidu.comc排查

    ping 百度出现:(ping www.baidu.com) "ping: unknown host www.baidu.com"    1.ping 网关确定是否连接上路由器,并 ...

  2. PyQt5入门教程

    原文链接:https://blog.csdn.net/azuremouse/article/details/90338961 问题记录: 1. pip 安装时速度太慢, 需要使用国内镜像 pip in ...

  3. MySQL示例数据库导入_1

    做个测试需要有适当量的数据库,于是找到了下面这个MySQL(超过30w记录), 1)先Git clone https://github.com/datacharmer/test_db         ...

  4. Excel常用公式大全

    公式是单个或多个函数的结合运用. AND “与”运算,返回逻辑值,仅当有参数的结果均为逻辑“真(TRUE)”时返回逻辑“真(TRUE)”,反之返回逻辑“假(FALSE)”. 条件判断 AVERAGE ...

  5. Android学习笔记颜色资源文件

    资源文件目录 颜色资源文件格式 colors.xml <?xml version="1.0" encoding="utf-8"?> <reso ...

  6. MyBatis一对多嵌套list返回结果集以及分页查询问题处理

    这两天在整理原有系统接口时,遇到后端的人员-角色-菜单的权限接口没有进行连表的关联查询操作,前端拿数据非常不方便,现在将接口相关sql进行修改并让前端可以一次性拿到想要的数据 原有的单表简单sql: ...

  7. leetcode1028 从先序遍历还原二叉树 python 100%内存 一次遍历

    1028. 从先序遍历还原二叉树 python 100%内存 一次遍历     题目 我们从二叉树的根节点 root 开始进行深度优先搜索. 在遍历中的每个节点处,我们输出 D 条短划线(其中 D 是 ...

  8. VulnHub PowerGrid 1.0.1靶机渗透

    ​本文首发于微信公众号:VulnHub PowerGrid 1.0.1靶机渗透,未经授权,禁止转载. 难度评级:☆☆☆☆☆官网地址:https://download.vulnhub.com/power ...

  9. 【SpringBoot MQ 系列】RabbitListener 消费基本使用姿势介绍

    [MQ 系列]RabbitListener 消费基本使用姿势介绍 之前介绍了 rabbitmq 的消息发送姿势,既然有发送,当然就得有消费者,在 SpringBoot 环境下,消费可以说比较简单了,借 ...

  10. SQL注入之sqlmap进阶

    上一篇我们对sqlmap进行简单的介绍,并介绍了一些·sqlmap的基础用法,这篇让我们来更深入的了解一下sqlmap,了解一下它的强大功能. 探测等级 参数为 --level 在sqlmap中一共有 ...