scrapy中选择器用法

一、Selector选择器介绍

python从网页中提取数据常用以下两种方法:

  • lxml:基于ElementTree的XML解析库(也可以解析HTML),不是python的标准库
  • BeautifulSoup:基于HTML代码的解析库, 对不良标记的处理非常合理,速度上有所欠缺

在scrapy中可以使用上述两种方法进行网页解析,但是scrapy本身也提供了一套提取数据的机制,即selector选择器,它通过特定的xpath或者CSS表达式来选择网页中的某个部分,Scrapy选择器构建 lxml 库之上。

在scrapy中内置了一个选择器类Selector,可以自己构建Selector对象,然后通过对象的xpath方法或css方法进行选择。

  • from scrapy.selector import Selector
  • s = Selector(text = doc)   #创建Selector对象
>>> from scrapy.selector import Selector
>>> p = '<p>I am a para</p>'
>>> s = Selector(text = p)
>>> s
<Selector xpath=None data='<html><body><p>I am a para</p></body>...'>
>>> s.xpath('//p')
[<Selector xpath='//p' data='<p>I am a para</p>'>]
>>> s.css('p::text')
[<Selector xpath='descendant-or-self::p/text()' data='I am a para'>]

但是实际爬虫工作中并不需要自己创建Selector对象,因为scrapy的爬取结果response中内置了Selector对象,可通过response.selector.xpath()和response.selector.css()进行选择。同时response也内置了xpath()方法和css()方法,因此可直接通过response.xpath()和response.css()进行选择。

从上述示例可以看出,通过xpath和css返回的直接结果为选择器列表,每个选择器都包含使用该语法的格式,因此需要在选择后再使用extract()和extract_first()去掉格式提取实际的网页内容,extract_first()相当于[0].extract()或者extract()[0]。

>>> s.xpath('//p').extract()
['<p>I am a para</p>']
>>> s.css('p::text').extract()
['I am a para']
>>> s.css('p::text').extract_first()
'I am a para'

 

二、通过xpath选择

xpath四种基本用法

response.xpath("//标签[@属性='属性值']").extract()    #返回匹配到的元素节点,结果为列表
response.xpath('//标签/text()').extract()                       #返回匹配到的元素节点的文本,结果为列表
response.xpath('//标签/@属性').extract()                    #返回匹配到的元素节点的属性值,结果为列表
response.xpath('//标签').re('匹配规则')         #对返回的结果通过正则表达式进行提取

以scrapy shell https://docs.scrapy.org/en/latest/_static/selectors-sample1.html进入shell进行演示。

1.选择标签

In [8]: response.xpath("//title")
Out[8]: [<Selector xpath='//title' data='<title>Example website</title>'>]

In [9]: response.xpath("//a[@href='image2.html']")
Out[9]: [<Selector xpath="//a[@href='image2.html']" data='<a href="image2.html">Name: My image ...'>]

In [10]: response.xpath("//a[@href='image2.html']").extract()
Out[10]: ['<a href="image2.html">Name: My image 2 <br><img src="image2_thumb.jpg"></a>']

不论匹配的结果有几个,使用extract()得到的都是一个列表,而使用extract_first()会将匹配到的第一个元素提取出来。由于extract()得到的结果为列表,因此可在xpath后的结果通过[0]选择第一个元素再extract()提取,也可以在extract()提取后再通过[0]选择第一个元素。

In [11]: response.xpath("//a").extract_first()
Out[11]: '<a href="image1.html">Name: My image 1 <br><img src="image1_thumb.jpg"></a>'
In [12]: response.xpath("//a")[0].extract()
Out[12]: '<a href="image1.html">Name: My image 1 <br><img src="image1_thumb.jpg"></a>'
In [13]: response.xpath("//a").extract()[0]
Out[13]: '<a href="image1.html">Name: My image 1 <br><img src="image1_thumb.jpg"></a>'

 

extract()和extract_first()还可以使用参数default='***',表示结果为空时的返回值

In [20]: response.xpath("//titll").extract_first(default='None return')
Out[20]: 'None return'

 

2.选择标签的文本

In [23]: response.xpath("//title/text()").extract_first()
Out[23]: 'Example website'

 

3.选择标签的属性值

In [25]: response.xpath("//a[@href='image3.html']/img/@src").extract_first()
Out[25]: 'image3_thumb.jpg'

  

4.xpath选择后的正则匹配

In [92]: response.xpath('//a/text()').re('Name:(.*)')
Out[92]:
[' My image 1 ',
 ' My image 2 ',
 ' My image 3 ',
 ' My image 4 ',
 ' My image 5 ']

 

 

三、通过css选择

css四种基本用法

response.css("标签[属性='属性值']").extract() #返回匹配到的元素节点,结果为列表
response.css("标签::text").extract() #返回匹配到的元素节点的文本,结果为列表
response.css("标签::attr(属性)") .extract() #返回匹配到的元素节点的属性值,结果为列表
response.css("标签").re('匹配规则') #对返回的结果通过正则表达式进行提取

1.选择标签

In [26]: response.css('title')
Out[26]: [<Selector xpath='descendant-or-self::title' data='<title>Example website</title>'>]
In [27]: response.css("a[href='image2.html']")
Out[27]: [<Selector xpath="descendant-or-self::a[@href = 'image2.html']" data='<a href="image2.html">Name: My image ...'>]
In [28]: response.css("a[href='image2.html']").extract_first()
Out[28]: '<a href="image2.html">Name: My image 2 <br><img src="image2_thumb.jpg"></a>'

 

2.选择标签的文本

In [51]: response.css("title::text").extract_first()
Out[51]: 'Example website'

 

3.选择标签的属性值

In [52]: response.css("a[href='image3.html']").css("img::attr(src)").extract_first()
Out[52]: 'image3_thumb.jpg'

 

4.css选择后的正则匹配

In [93]: response.css('a::attr(href)').re('(.*).html')
Out[93]: ['image1', 'image2', 'image3', 'image4', 'image5']

 

 

 

posted @ 2019-09-02 17:10  Forever77  阅读(566)  评论(0编辑  收藏  举报