python 第二周(第十天) 我的python成长记 一个月搞定python数据挖掘!(18) -mongodb

1. 首先导入工具
from scrapy.selector import Selector

2. selectors的使用
实例:response.selector.xpath('//span/text()').extract()

(1)选择title标签中text的文本内容
response.selector.xpath('//title/text()')
提供两个更简单的方法
response.xpath('//title/text()')
response.css('title::text')
例子:
response.css('img').xpath('@src').extract()
response.xpath('//div[@></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']