爬虫入门到精通_框架篇18(Scrapy中选择器用法)_sector,xpath,css,re

官方文档

Using selectors

To explain how to use the selectors we’ll use the Scrapy shell (which provides interactive testing) and an example page located in the Scrapy documentation server:
https://docs.scrapy.org/en/latest/_static/selectors-sample1.html
在这里插入图片描述

<!DOCTYPE html>

<html>
  <head>
    <base href='http://example.com/' />
    <title>Example website</title>
  </head>
  <body>
    <div id='images'>
      <a href='image1.html'>Name: My image 1 <br /><img src='image1_thumb.jpg' alt='image1'/></a>
      <a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' alt='image2'/></a>
      <a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' alt='image3'/></a>
      <a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' alt='image4'/></a>
      <a href='image5.html'>Name: My image 5 <br /><img src='image5_thumb.jpg' alt='image5'/></a>
    </div>
  </body>
</html>

进入命令行交互模式:

scrapy shell https://docs.scrapy.org/en/latest/_static/selectors-sample1.html

在这里插入图片描述
输入

response.selector

输出:request内置的selector选择器
在这里插入图片描述

XPath选择器

let’s construct an XPath for selecting the text inside the title tag:

response.xpath("//title/text()")

输出选择器与内容.
在这里插入图片描述

css选择器

response.css("title::text").get()

在这里插入图片描述

xpath和css的运用

xpath查找images标签

response.xpath('//div[@id="images"]')

在这里插入图片描述

response.xpath('//div[@id="images"]').css("img")

在这里插入图片描述
css可以用::attr()获取属性:

response.xpath('//div[@id="images"]').css("img::attr(src)").extract()

在这里插入图片描述
default:查不到内容返回default里内容
在这里插入图片描述
href标签:
在这里插入图片描述

contains

找属性名称包含image的所有的超链接可以使用contains选项,第一个参数是属性名,第二个属性是要查找的值

response.xpath('//a[contains(@href,"image")]/@href').extract()

在这里插入图片描述
CSS的写法:

response.css('a[href*=image]::attr(href)').extract()

在这里插入图片描述
假如我们要选择所有a标签里的img里面的src属性,用上contains:

response.xpath('//a[contains(@href,"image")]/img/@src').extract()

在这里插入图片描述
CSS:注意[]之后要有空格

response.css('a[href*=image] img::attr(src)').extract()

在这里插入图片描述

正则表达式

提取内容
在这里插入图片描述
提取冒号后的内容,就需要正则表达式了,注意,\用来对:进行转义。

 response.css('a::text').re('Name\:(.*)')

在这里插入图片描述
与extract()方法类似,re也提供了取得列表中第一个元素的方法:re_first()

response.css('a::text').re_first('Name\:(.*)')

在这里插入图片描述
进一步地,可以使用strip()方法,去掉返回结果中前后的空格:

response.css('a::text').re_first('Name\:(.*)').strip()

在这里插入图片描述

小结

response为我们提供了几个提取方法:

  • xpath
  • CSS
  • re

返回的结果都是Selector类型,可以进行嵌套循环。
a) 对css来说:

  • 获取a标签中的文本内容:response.css(‘a::text’)
  • 获取a标签中的某个属性:response.css(‘a::attr(属性)’)

(b)对xpath来说:

  • 获取a标签中的文本内容:response.xpath(‘//a/text()’)
  • 获取a标签中的某个属性:response.xpath(‘//a/@href’)

两种选择方法,写法不同,效果类似。

要从selector变为数据,则在后面加上.extract() 或 .extract()_first() 或.extract()[x](x为list中元素的下标)。
如果要提取更具体的信息,可以用正则表达式的方法,在后面加上 .re() 或 .re()_first 进行嵌套选择。

Scrapy是一个基于Python的爬虫框架,它可以帮助我们快速高效地抓取网站数据。在这里,我将介绍Scrapy的基本用法,让您能够快速入门。 安装Scrapy ----------------------- 在安装Scrapy之前,我们需要先安装Python。然后,我们可以通过以下命令来安装Scrapy: ``` pip install scrapy ``` 创建Scrapy项目 ----------------------- 创建Scrapy项目的命令是: ``` scrapy startproject project_name ``` 这个命令将会在当前目录下创建一个名为project_name的文件夹,其包含了Scrapy项目的基本结构。 编写Spider ----------------------- 在Scrapy,Spider是用来定义爬取网站的规则的。我们可以通过以下命令来创建一个Spider: ``` scrapy genspider spider_name domain_name ``` 其,spider_name是我们自己定义的Spider名称,domain_name是我们要抓取的网站域名。 接下来,我们需要在Spider定义如何爬取网站。这里我们以爬取“http://quotes.toscrape.com/”网站上的名言警句为例。我们可以在Spider定义如下规则: ```python import scrapy class QuotesSpider(scrapy.Spider): name = "quotes" start_urls = [ 'http://quotes.toscrape.com/page/1/', 'http://quotes.toscrape.com/page/2/', ] def parse(self, response): for quote in response.css('div.quote'): yield { 'text': quote.css('span.text::text').get(), 'author': quote.css('span small::text').get(), 'tags': quote.css('div.tags a.tag::text').getall(), } next_page = response.css('li.next a::attr(href)').get() if next_page is not None: yield response.follow(next_page, self.parse) ``` 在上述代码,我们首先定义了Spider的名称,接着定义了我们要爬取的起始URL,最后定义了如何解析网页的函数parse()。在parse()函数,我们使用了Scrapy选择器来提取网页的名言警句,并将其保存到字典。接着,我们使用response.follow()函数来获取下一页的URL,并继续解析。 运行Spider ----------------------- 要运行我们刚才创建的Spider,我们可以使用以下命令: ``` scrapy crawl spider_name ``` 其,spider_name是我们之前创建的Spider名称。 Scrapy会自动去抓取我们定义的起始URL,并根据我们定义的规则来解析网页。解析完成后,Scrapy会将结果保存到我们指定的位置。 总结 ----------------------- Scrapy是一个非常强大的Python爬虫框架,它可以帮助我们快速高效地抓取网站数据。在本教程,我们介绍了Scrapy项目的创建、Spider的定义以及如何运行Spider。如果您想更深入地学习Scrapy,可以参考官方文档:https://docs.scrapy.org/en/latest/。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值