scrapy的name变量_scrapy xpath 常用方法

[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()

```

您好!要使用Python的Scrapy框架来爬取古诗词,您可以按照以下步骤进行操作: 1. 首先,确保您已经安装了Scrapy框架。可以使用以下命令来安装Scrapy: ```bash pip install scrapy ``` 2. 创建一个新的Scrapy项目。在命令行中进入您希望创建项目的目录,并运行以下命令: ```bash scrapy startproject poetry_crawler ``` 这将在当前目录下创建一个名为"poetry_crawler"的新文件夹,其中包含Scrapy项目的文件结构。 3. 进入项目文件夹并创建一个新的Spider。在命令行中运行以下命令: ```bash cd poetry_crawler scrapy genspider poetry_spider example.com ``` 这将在名为"poetry_spider.py"的文件中生成一个基本的Spider示例。 4. 打开"poetry_spider.py"文件,并对Spider进行修改。您可以使用XPath或CSS选择器来定位和提取古诗词的信息。例如,以下代码演示了如何使用XPath选择器来提取古诗词的标题和内容: ```python import scrapy class PoetrySpider(scrapy.Spider): name = 'poetry_spider' start_urls = ['http://example.com'] def parse(self, response): titles = response.xpath('//h1/text()').getall() contents = response.xpath('//div[@class="content"]/text()').getall() for title, content in zip(titles, contents): yield { 'title': title, 'content': content } ``` 5. 如果要爬取其他网站上的古诗词,您需要修改"start_urls"变量,并调整XPath选择器以适应不同的网页结构。 6. 运行爬虫并输出结果。在命令行中运行以下命令: ```bash scrapy crawl poetry_spider -o poems.json ``` 这将运行您的爬虫,并将提取到的古诗词保存到名为"poems.json"的JSON文件中。 请注意,爬取网站时需要遵守相关的法律和规定,确保您有合法的许可和权限。 希望这可以帮助到您!如有任何问题,请随时向我提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值