常用方法
1. 爬虫方法
方法名 | 作用描述 | 示例 |
|---|
start_requests()
| 生成初始请求,可以自定义请求头、请求方法等。 | yield scrapy.Request(url, callback=self.parse)
|
parse(response)
| 处理响应并提取数据,是爬虫的核心方法。 | yield {'title': response.css('h1::text').get()}
|
follow(url, callback)
| 自动处理相对 URL 并生成新的请求,用于分页或链接跳转。 | yield response.follow(next_page, callback=self.parse)
|
closed(reason)
| 爬虫关闭时调用,用于清理资源或记录日志。 | def closed(self, reason): print('Spider closed:', reason)
|
log(message)
| 记录日志信息。 | self.log('This is a log message')
|
2. 数据提取方法
方法名 | 作用描述 | 示例 |
|---|
response.css(selector)
| 使用 CSS 选择器提取数据。 | title = response.css('h1::text').get()
|
response.xpath(selector)
| 使用 XPath 选择器提取数据。 | title = response.xpath('//h1/text()').get()
|
get()
| 从 SelectorList 中提取第一个匹配的结果(字符串)。 | title = response.css('h1::text').get()
|
getall()
| 从 SelectorList 中提取所有匹配的结果(列表)。 | titles = response.css('h1::text').getall()
|
attrib
| 提取当前节点的属性。 | link = response.css('a::attr(href)').get()
|
3. 请求与响应方法
方法名 | 作用描述 | 示例 |
|---|
scrapy.Request(url, callback, method, headers, meta)
| 创建一个新的请求。 | yield scrapy.Request(url, callback=self.parse, headers=headers)
|
response.url
| 获取当前响应的 URL。 | current_url = response.url
|
response.status
| 获取响应的状态码。 | if response.status == 200: print('Success')
|
response.meta
| 获取请求中传递的额外数据。 | value = response.meta.get('key')
|
response.headers
| 获取响应的头信息。 | content_type = response.headers.get('Content-Type')
|
4. 中间件与管道方法
方法名 | 作用描述 | 示例 |
|---|
process_request(request, spider)
| 在请求发送前处理请求(下载器中间件)。 | request.headers['User-Agent'] = 'Mozilla/5.0'
|
process_response(request, response, spider)
| 在响应返回后处理响应(下载器中间件)。 | if response.status == 403: return request.replace(dont_filter=True)
|
process_item(item, spider)
| 处理提取的数据(管道)。 | if item['price'] < 0: raise DropItem('Invalid price')
|
open_spider(spider)
| 爬虫启动时调用(管道)。 | def open_spider(self, spider): self.file = open('items.json', 'w')
|
close_spider(spider)
| 爬虫关闭时调用(管道)。 | def close_spider(self, spider): self.file.close()
|
5. 工具与扩展方法
方法名 | 作用描述 | 示例 |
|---|
scrapy shell
| 启动交互式 Shell,用于调试和测试选择器。 | scrapy shell 'http://example.com'
|
scrapy crawl <spider_name>
| 运行指定的爬虫。 | scrapy crawl myspider -o output.json
|
scrapy check
| 检查爬虫代码的正确性。 | scrapy check
|
scrapy fetch
| 下载指定 URL 的内容。 | scrapy fetch 'http://example.com'
|
scrapy view
| 在浏览器中查看 Scrapy 下载的页面。 | scrapy view 'http://example.com'
|
6. 常用设置(settings.py)
设置项 | 作用描述 | 示例 |
|---|
USER_AGENT
| 设置请求头中的 User-Agent。 | USER_AGENT = 'Mozilla/5.0'
|
ROBOTSTXT_OBEY
| 是否遵守 robots.txt 规则。 | ROBOTSTXT_OBEY = False
|
DOWNLOAD_DELAY
| 设置下载延迟,避免过快请求。 | DOWNLOAD_DELAY = 2
|
CONCURRENT_REQUESTS
| 设置并发请求数。 | CONCURRENT_REQUESTS = 16
|
ITEM_PIPELINES
| 启用管道。 | ITEM_PIPELINES = {'myproject.pipelines.MyPipeline': 300}
|
AUTOTHROTTLE_ENABLED
| 启用自动限速扩展。 | AUTOTHROTTLE_ENABLED = True
|
7. 其他常用方法
方法名 | 作用描述 | 示例 |
|---|
response.follow_all(links, callback)
| 批量处理链接并生成请求。 | yield from response.follow_all(links, callback=self.parse)
|
response.json()
| 将响应内容解析为 JSON 格式。 | data = response.json()
|
response.text
| 获取响应的文本内容。 | html = response.text
|
response.selector
| 获取响应内容的 Selector 对象。 | title = response.selector.css('h1::text').get()
|
以上表格列出了 Scrapy 中常用的方法及其作用。这些方法涵盖了爬虫开发的各个方面,包括请求生成、数据提取、中间件处理、管道操作等。通过掌握这些方法,你可以高效地编写和管理 Scrapy 爬虫。如果需要更详细的功能,可以参考 Scrapy 官方文档。
官网:https://docs.scrapy.org/en/latest/
中文:https://docs.scrapy.net.cn/en/latest/intro/tutorial.html#our-first-spider
评论