[TOC]
# 1、命令行启动scrapy
```
# scrapy shell "http://www.baidu.com/"
```
然后使用 respone 变量, response 变量里面是请求URL后的返回信息。
```
divs = response.xpath('//div') 全局搜索
p = divs.xpath('.//p') 在 divs 的基础上全局搜素
p2 = divs.xpath('p') 提取所有直接子p标签
```
# 2、全局搜索
```
response.xpath('//div[@class="row header-box"]/text()').extract_first()
```
`extract_first()` 如果有多个结果则获取第一个,可以避免使用 list 下标访问出错
# 3、获取文本内容的三个函数
```
.extract()
.extract_first()
.extract_first(default='not-found')
```
# 4、获取标签内的文本内容
使用 `text()` 函数、`extract_first()` 函数。
```
response.xpath('//title/text()').extract_first()
```
# 5、根据标签属性值获取内容
- 获取a标签下的 href 值
```
response.xpath('a/@href')[1].extract_first()
```
- 全局获取 `a` 标签中, `href` 属性包含字符串 `"image"` 的 `href` 属性值
```
response.xpath('//a[contains(@href, "image")]/@href').extract_first()
```
- 全局获取 `a` 标签中, `href` 属性包含字符串 `"image"`, 并获取 `a` 标签下的 `img` 标签的 `src` 属性值
```
response.xpath('//a[contains(@href, "image")]/img/@src').extract_first()
```
# 6、内容匹配正则表达式
- 正则表达式匹配,结果已经分组 list
```
response.xpath('//a[contains(@href, "image")]/text()').re(r'Name:\s*(.*)')
```
- 正则表达式匹配,结果已经分组 list , 使用 `re_first()`
```
response.xpath('//a[contains(@href, "image")]/text()').re_first(r'Name:\s*(.*)')
```
# 7、xpath内部使用正则表达式
```
全局搜索 li 标签,它的 class 属性满足正则表达式 "item-\d$" 的 li 标签
response.xpath('//li[re:test(@class, "item-\d$")]//@href').extract()
```
# 8、变量使用
- $val的值由随后的 `val='images'` 传递进去
```
response.xpath('//div[@id=$val]/a/text()', val='images').extract_first()
```
- 搜索 div 标签中包含有5个 a 标签的 div 的 id
```
response.xpath('//div[count(a)=$cnt]/@id', cnt=5).extract_first()
```