第三百二十五节,web爬虫,scrapy模块标签选择器下载图片,以及正则匹配标签

标签选择器对象

HtmlXPathSelector()创建标签选择器对象,参数接收response回调的html对象
需要导入模块:from scrapy.selector import HtmlXPathSelector

select()标签选择器方法,是HtmlXPathSelector里的一个方法,参数接收选择器规则,返回列表元素是一个标签对象

extract()获取到选择器过滤后的内容,返回列表元素是内容

选择器规则

  //x 表示向下查找n层指定标签,如://div 表示查找所有div标签
  /x 表示向下查找一层指定的标签
  /@x 表示查找指定属性,可以连缀如:@id @src
  [@class="class名称"] 表示查找指定属性等于指定值的标签,可以连缀 ,查找class名称等于指定名称的标签
  /text() 获取标签文本类容
  [x] 通过索引获取集合里的指定一个元素

获取指定的标签对象

# -*- coding: utf-8 -*-
import scrapy       #导入爬虫模块
from scrapy.selector import HtmlXPathSelector  #导入HtmlXPathSelector模块
from urllib import request                     #导入request模块
import osclass AdcSpider(scrapy.Spider):name = 'adc'                                        #设置爬虫名称allowed_domains = ['www.shaimn.com']start_urls = ['http://www.shaimn.com/xinggan/']def parse(self, response):hxs = HtmlXPathSelector(response)               #创建HtmlXPathSelector对象,将页面返回对象传进去
items = hxs.select('//div[@class="showlist"]/li')  #标签选择器,表示获取所有class等于showlist的div,下面的li标签print(items)                                       #返回标签对象

循环获取到每个li标签里的子标签,以及各种属性或者文本

# -*- coding: utf-8 -*-
import scrapy       #导入爬虫模块
from scrapy.selector import HtmlXPathSelector  #导入HtmlXPathSelector模块
from urllib import request                     #导入request模块
import osclass AdcSpider(scrapy.Spider):name = 'adc'                                        #设置爬虫名称allowed_domains = ['www.shaimn.com']start_urls = ['http://www.shaimn.com/xinggan/']def parse(self, response):hxs = HtmlXPathSelector(response)               #创建HtmlXPathSelector对象,将页面返回对象传进去
items = hxs.select('//div[@class="showlist"]/li')  #标签选择器,表示获取所有class等于showlist的div,下面的li标签# print(items)                                     #返回标签对象for i in range(len(items)):                        #根据li标签的长度循环次数title = hxs.select('//div[@class="showlist"]/li[%d]//img/@alt' % i).extract()   #根据循环的次数作为下标获取到当前li标签,下的img标签的alt属性内容src = hxs.select('//div[@class="showlist"]/li[%d]//img/@src' % i).extract()     #根据循环的次数作为下标获取到当前li标签,下的img标签的src属性内容if title and src:print(title,src)  #返回类容列表

将获取到的图片下载到本地

urlretrieve()将文件保存到本地,参数1要保存文件的src,参数2保存路径
urlretrieve是urllib下request模块的一个方法,需要导入from urllib import request

# -*- coding: utf-8 -*-
import scrapy       #导入爬虫模块
from scrapy.selector import HtmlXPathSelector  #导入HtmlXPathSelector模块
from urllib import request                     #导入request模块
import osclass AdcSpider(scrapy.Spider):name = 'adc'                                        #设置爬虫名称allowed_domains = ['www.shaimn.com']start_urls = ['http://www.shaimn.com/xinggan/']def parse(self, response):hxs = HtmlXPathSelector(response)               #创建HtmlXPathSelector对象,将页面返回对象传进去
items = hxs.select('//div[@class="showlist"]/li')  #标签选择器,表示获取所有class等于showlist的div,下面的li标签# print(items)                                     #返回标签对象for i in range(len(items)):                        #根据li标签的长度循环次数title = hxs.select('//div[@class="showlist"]/li[%d]//img/@alt' % i).extract()   #根据循环的次数作为下标获取到当前li标签,下的img标签的alt属性内容src = hxs.select('//div[@class="showlist"]/li[%d]//img/@src' % i).extract()     #根据循环的次数作为下标获取到当前li标签,下的img标签的src属性内容if title and src:# print(title[0],src[0])                                                    #通过下标获取到字符串内容file_path = os.path.join(os.getcwd() + '/img/', title[0] + '.jpg')          #拼接图片保存路径request.urlretrieve(src[0], file_path)                          #将图片保存到本地,参数1获取到的src,参数2保存路径

xpath()标签选择器,是Selector类里的一个方法,参数是选择规则【推荐】

选择器规则同上

selector()创建选择器类,需要接受html对象
需要导入:from scrapy.selector import Selector

# -*- coding: utf-8 -*-
import scrapy       #导入爬虫模块
from scrapy.selector import HtmlXPathSelector  #导入HtmlXPathSelector模块
from scrapy.selector import Selectorclass AdcSpider(scrapy.Spider):name = 'adc'                                        #设置爬虫名称allowed_domains = ['www.shaimn.com']start_urls = ['http://www.shaimn.com/xinggan/']def parse(self, response):items = Selector(response=response).xpath('//div[@class="showlist"]/li').extract()# print(items)                                     #返回标签对象for i in range(len(items)):title = Selector(response=response).xpath('//div[@class="showlist"]/li[%d]//img/@alt' % i).extract()src = Selector(response=response).xpath('//div[@class="showlist"]/li[%d]//img/@src' % i).extract()print(title,src)

正则表达式的应用

正则表达式是弥补,选择器规则无法满足过滤情况时使用的,

分为两种正则使用方式

  1、将选择器规则过滤出来的结果进行正则匹配

  2、在选择器规则里应用正则进行过滤

1、将选择器规则过滤出来的结果进行正则匹配,用正则取最终内容

最后.re('正则')

# -*- coding: utf-8 -*-
import scrapy       #导入爬虫模块
from scrapy.selector import HtmlXPathSelector  #导入HtmlXPathSelector模块
from scrapy.selector import Selectorclass AdcSpider(scrapy.Spider):name = 'adc'                                        #设置爬虫名称allowed_domains = ['www.shaimn.com']start_urls = ['http://www.shaimn.com/xinggan/']def parse(self, response):items = Selector(response=response).xpath('//div[@class="showlist"]/li//img')[0].extract()print(items)                                     #返回标签对象items2 = Selector(response=response).xpath('//div[@class="showlist"]/li//img')[0].re('alt="(\w+)')print(items2)# <img src="http://www.shaimn.com/uploads/170724/1-1FH4221056141.jpg" alt="人体艺术mmSunny前凸后翘性感诱惑写真">
# ['人体艺术mmSunny前凸后翘性感诱惑写真']

2、在选择器规则里应用正则进行过滤

[re:正则规则]

# -*- coding: utf-8 -*-
import scrapy       #导入爬虫模块
from scrapy.selector import HtmlXPathSelector  #导入HtmlXPathSelector模块
from scrapy.selector import Selectorclass AdcSpider(scrapy.Spider):name = 'adc'                                        #设置爬虫名称allowed_domains = ['www.shaimn.com']start_urls = ['http://www.shaimn.com/xinggan/']def parse(self, response):items = Selector(response=response).xpath('//div').extract()# print(items)                                     #返回标签对象items2 = Selector(response=response).xpath('//div[re:test(@class, "showlist")]').extract()  #正则找到div的class等于showlist的元素print(items2)

转载于:https://www.cnblogs.com/adc8868/p/7231107.html

第三百二十五节,web爬虫,scrapy模块标签选择器下载图片,以及正则匹配标签...相关推荐

  1. 第三百二十六节,web爬虫,scrapy模块,解决重复ur——自动递归url

    第三百二十六节,web爬虫,scrapy模块,解决重复url--自动递归url 一般抓取过的url不重复抓取,那么就需要记录url,判断当前URL如果在记录里说明已经抓取过了,如果不存在说明没抓取过 ...

  2. 第三百二十四节,web爬虫,scrapy模块介绍与使用

    第三百二十四节,web爬虫,scrapy模块介绍与使用 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中. 其最初是为 ...

  3. 第一百二十六节,JavaScript,XPath操作xml节点

    第一百二十六节,JavaScript,XPath操作xml节点 学习要点: 1.IE中的XPath 2.W3C中的XPath 3.XPath跨浏览器兼容 XPath是一种节点查找手段,对比之前使用标准 ...

  4. 计算机专业必玩游戏,游戏推荐 篇三百八十五:非常好玩的机甲类游戏推荐

    游戏推荐 篇三百八十五:非常好玩的机甲类游戏推荐 2020-12-14 15:46:07 20点赞 77收藏 14评论 创作立场声明:哈喽,各位值友的小伙伴们大家好呀,我是你们的老朋友大白(●-●), ...

  5. 第三百八十九节,Django+Xadmin打造上线标准的在线教育平台—列表筛选结合分页...

    第三百八十九节,Django+Xadmin打造上线标准的在线教育平台-列表筛选结合分页 根据用户的筛选条件来结合分页 实现原理就是,当用户点击一个筛选条件时,通过get请求方式传参将筛选的id或者值, ...

  6. web爬虫,scrapy模块标签选择器下载图片,以及正则匹配标签

    标签选择器对象 HtmlXPathSelector()创建标签选择器对象,参数接收response回调的html对象 需要导入模块:from scrapy.selector import HtmlXP ...

  7. nmcli命令详解_【高新课堂】第一百二十五期Liunx必备命令

    点击上方"蓝字"关注我们吧! Liunx系统启动默认为字符界面,一般不会启用图像界面,所以对命令行的熟练程度能更加高效.便捷的管理Liunx服务器. 这节课向读者介绍Liunx系统 ...

  8. 第二百七十五节,MySQL数据库安装和介绍

    MySQL数据库安装 一.概述 1.什么是数据库 ? 答:数据的仓库,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Server等 ? 答:他们均是一种 ...

  9. 贩妖记 第三百七十五章,教授笔记

    这是许天旺教授的笔记本,也就是说他曾经在这里生活过一段时间,但随后抛下了一切不知去向.从现场的生活状态来看,他当时过的很艰苦,而且走的很匆忙. 翻开笔记本,一开始记录的是他对鬼城的一些研究. 开头的第 ...

最新文章

  1. 异步获取邮件推送结果
  2. eclipse格式化代码
  3. python小技巧:获取字典中值最大者的key
  4. vue.js computedmethod
  5. linux aio拷贝文件,Linux通过AIO进行异步读文件
  6. UserData的诡异bug
  7. mysql查询语句出现sending data耗时解决
  8. 突破信息封锁,快速建立镜像网站
  9. SQLyog客户端使用教程
  10. 前端项目打包后生成的chunk-vendors文件过大,导致加载太慢
  11. Android代码中获取Drawable对象
  12. android 评论发表情,安卓微信朋友圈怎么评论发表情包 微信朋友圈评论发表情包方法...
  13. HDU-4699 对顶栈
  14. webpack5之webpack-dev-server(实时重新加载(live reloading)
  15. 小六壬(邵一尘大师博客有介绍)
  16. 《历术甲子篇》冬至合朔表
  17. 实战分析!三面蚂蚁核心金融部,稳进大厂
  18. Linux查看被拦截的日志,imperva拦截日志的实时报警
  19. 出海欧洲《通用数据保护条例》解读,附GDPR白皮书下载
  20. C++的strcmp

热门文章

  1. Qt之系统托盘你学清楚了吗?
  2. Linux开胃菜,静态网络设置
  3. 云栖科技评论第71期:数字经济时代,需要“新大脑”
  4. python程序设计基础山东联盟化工集团有限公司_02680061:-智慧树C语言程序设计(山东联盟-青岛科技大学)章节答案...
  5. 【2022HWS硬件安全冬令营预选赛 misc】BadPDF+gogogo WriteUp
  6. jQuery 特效网址(特效大全)
  7. 【AI视野·今日Robot 机器人论文速览 第二十七期】Thu, 4 Nov 2021
  8. [设计模式] —— Builder 生成器模式
  9. 【笔记】LR响应时间
  10. 大学选修课实用计算机技术,【原创】写给大一学弟学妹:大一应该选修哪些实用有趣的选修课?...