官方示例源码
<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' /></a>
   <a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
   <a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
   <a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
   <a href='image5.html'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
  </div>
 </body>
</html>

# scrapy shell http://doc.scrapy.org/en/latest/_static/selectors-sample1.html

>>> response.xpath('//title/text()')
[<Selector (text) xpath=//title/text()>]

>>> response.css('title::text')
[<Selector (text) xpath=//title/text()>]

>>> response.css('title::text').extract()
[u'Example website']

>>> response.xpath('//title/text()').extract()
[u'Example website']

>>> response.xpath('//base/@href').extract()
[u'http://example.com/']

>>> response.css('base::attr(href)').extract()
[u'http://example.com/']

>>> response.xpath('//a[contains(@href, "image")]/@href').extract()
[u'image1.html',
 u'image2.html',
 u'image3.html',
 u'image4.html',
 u'image5.html']

>>> response.css('a[href*=image]::attr(href)').extract()
[u'image1.html',
 u'image2.html',
 u'image3.html',
 u'image4.html',
 u'image5.html']

>>> response.xpath('//a/@href')]').extract()
['image1.html',
 'image2.html',
 'image3.html',
 'image4.html',
 'image5.html']

>>> response.css('a::attr(href)').extract()
['image1.html',
 'image2.html',
 'image3.html',
 'image4.html',
 'image5.html']

>>> response.xpath('//div[@id="image"]').css('img::attr(src)').extract()
['image1_thumb.jpg',
 'image2_thumb.jpg',
 'image3_thumb.jpg',
 'image4_thumb.jpg',
 'image5_thumb.jpg']
 
>>> response.xpath('//div[@id="image"]').css('img::attr(src)').extract_first()
'image1_thumb.jpg'

# 默认值,查找不存在的元素,使用默认值
>>> response.xpath('//div[@id="image"]').css('img::attr(data-src)').extract_first(deafult='')
''

>>> response.xpath('//a[contains(@href, "image")]/img/@src').extract()
[u'image1_thumb.jpg',
 u'image2_thumb.jpg',
 u'image3_thumb.jpg',
 u'image4_thumb.jpg',
 u'image5_thumb.jpg']

>>> response.css('a[href*=image] img::attr(src)').extract()
[u'image1_thumb.jpg',
 u'image2_thumb.jpg',
 u'image3_thumb.jpg',
 u'image4_thumb.jpg',
 u'image5_thumb.jpg']
 
>>> links = response.xpath('//a[contains(@href, "image")]')
>>> links.extract()
[u'<a href="image1.html">Name: My image 1 <br><img src="data:image1_thumb.jpg"></a>',
 u'<a href="image2.html">Name: My image 2 <br><img src="data:image2_thumb.jpg"></a>',
 u'<a href="image3.html">Name: My image 3 <br><img src="data:image3_thumb.jpg"></a>',
 u'<a href="image4.html">Name: My image 4 <br><img src="data:image4_thumb.jpg"></a>',
 u'<a href="image5.html">Name: My image 5 <br><img src="data:image5_thumb.jpg"></a>']

>>> for index, link in enumerate(links):
        args = (index, link.xpath('@href').extract(), link.xpath('img/@src').extract())
        print 'Link number %d points to url %s and image %s' % args

Link number 0 points to url [u'image1.html'] and image [u'image1_thumb.jpg']
Link number 1 points to url [u'image2.html'] and image [u'image2_thumb.jpg']
Link number 2 points to url [u'image3.html'] and image [u'image3_thumb.jpg']
Link number 3 points to url [u'image4.html'] and image [u'image4_thumb.jpg']
Link number 4 points to url [u'image5.html'] and image [u'image5_thumb.jpg']

>>> response.xpath('//a/text()').extract()
['Name:My image 1',
 'Name:My image 2',
 'Name:My image 3',
 'Name:My image 4',
 'Name:My image 5']
 
>>> response.css('a::text').extract()
['Name:My image 1',
 'Name:My image 2',
 'Name:My image 3',
 'Name:My image 4',
 'Name:My image 5']
 
 
>>> response.xpath('//a[contains(@href, "image")]/@href').extract()
['image1.html',
 'image2.html',
 'image3.html',
 'image4.html',
 'image5.html']
 
>>> response.css('a[href*=image] img::attr(href)').extract()
['image1.html',
 'image2.html',
 'image3.html',
 'image4.html',
 'image5.html']

# 使用正则    
>>> response.xpath('//a[contains(@href, "image")]/text()').re(r'Name:\s*(.*)')
[u'My image 1',
 u'My image 2',
 u'My image 3',
 u'My image 4',
 u'My image 5']

>>> response.xpath('//a[contains(@href, "image")]/text()').re_first(r'Name:\s*(.*)')
'My image 1'

>>> response.xpath('//a/text()').re(r'Name:\s*(.*)')
['My image 1',
 'My image 2',
 'My image 3',
 'My image 4',
 'My image 5']

>>> response.xpath('//a/text()').re_first(r'Name:\s*(.*)')
'My image 1'

>>> response.css('a::text').re(r'Name:\s*(.*)')
['My image 1',
 'My image 2',
 'My image 3',
 'My image 4',
 'My image 5']

#使用strip()再次处理字符串中的空格,注意跟前面的相比较
re_first('Name:(.*)').strip()
re(r'Name:\s*(.*)')
>>> response.css('a::text').re_first('Name:(.*)').strip()
'My image 1'

# 获取所有的a标签超链接
>>> response.css('a').extract()
['<a href='image1.html'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>',
'<a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>',
'<a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>',
'<a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>',
'<a href='image5.html'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>']

>>> response.css('a').extract_first()
'<a href='image1.html'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>'

最新文章

  1. Android基础总结(六)
  2. 2016 windows安装phing:安装成功
  3. hibernate----N-1(一)
  4. js禁止用户右键等操作
  5. AndroidStudio 中的坑Error:(1, 0) Plugin is too old, please update to a more recent version, or set ANDROID_DAILY_OVERRID
  6. HDU 1671 Phone List(Trie的应用与内存释放)
  7. SQL常用方法整理
  8. c++ 从标注输入流读取行
  9. 14.8.9 Clustered and Secondary Indexes
  10. hadoop部署、启动全套过程
  11. Weblogic的集群
  12. redis 链表
  13. docker load 镜像时出现:open /var/lib/docker/tmp/docker-import-500852078/repositories: no such file or dir
  14. Apache生产配置
  15. sunzl is not in the sudoers file.This incident will be reported
  16. TF报错解决
  17. 细说python类2——类动态添加方法和slots(转)
  18. ESP-EYE V2.1 开发板 WINDOWS 10 开发入门
  19. Day15作业及默写
  20. [转]asp+oracle分页

热门文章

  1. Cant&#39;t call setState(or forceUpdate) on an unmount component. 报错的可能性原因
  2. jQuery--编辑表格
  3. bootstrap table 显示连续序号,分页有效
  4. ref 和out的区别
  5. 客户端JavaScript&#160;Ajax
  6. 用vue-cli快速构建项目
  7. bzoj1433[ZJOI2009]假期的宿舍(匈牙利)
  8. [App Store Connect帮助]三、管理 App 和版本(3)查找 App
  9. Linux搭建tomcat文件服务器
  10. Sql批量插入方法