使用 Scrapy 抓取网页数据

发布于:2025-02-11 ⋅ 阅读:(114) ⋅ 点赞:(0)
1. Scrapy 简介

Scrapy 是一个流行的 Python 爬虫框架,提供了强大的工具和灵活的扩展机制,用于高效抓取和处理网页数据。它支持异步 I/O,速度快且资源消耗低,非常适合大规模爬取任务。


2. 安装 Scrapy

确保你的 Python 环境版本在 3.7 或以上。

使用 pip 安装:

pip install scrapy

验证安装:

scrapy version


3. 创建 Scrapy 项目

创建一个新的 Scrapy 项目:

scrapy startproject myproject

目录结构:

myproject/
    scrapy.cfg
    myproject/
        __init__.py
        items.py
        middlewares.py
        pipelines.py
        settings.py
        spiders/
            __init__.py

4. 编写第一个爬虫

进入项目目录并生成爬虫:

scrapy genspider example example.com

生成的文件 spiders/example.py

import scrapy

class ExampleSpider(scrapy.Spider):
    name = 'example'
    allowed_domains = ['example.com']
    start_urls = ['http://example.com']

    def parse(self, response):
        self.log('Visited: ' + response.url)

运行爬虫:

scrapy crawl example


5. 提取数据:XPath 与 CSS 选择器

Scrapy 支持 XPath 和 CSS 选择器用于解析 HTML。以下是两种选择器的简单对比:

XPath 示例:

titles = response.xpath('//h1/text()').getall()

CSS 示例:

titles = response.css('h1::text').getall()

常用方法:

  • get(): 获取单个匹配的内容。
  • getall(): 获取所有匹配的内容。
  • extract_first(): 等价于 .get()
  • extract(): 等价于 .getall()

6. 保存数据

Scrapy 支持将数据导出为 JSON、CSV、XML 等格式。

在终端中导出数据:

scrapy crawl example -o output.json

将数据存储到管道: 编辑 pipelines.py 文件:

class MyProjectPipeline:

    def process_item(self, item, spider):

        with open('output.txt', 'a') as f:

            f.write(str(item) + '\n')

        return item

settings.py 中启用管道:

ITEM_PIPELINES = { 'myproject.pipelines.MyProjectPipeline': 300, }


7. 处理动态加载的页面

许多现代网站使用 JavaScript 动态渲染内容,Scrapy 默认无法处理这种情况。可以结合以下工具:

Scrapy-Splash:

  • 安装:

    pip install scrapy-splash

  • 配置: 在 settings.py 中添加:
    SPLASH_URL = 'http://localhost:8050'
    DOWNLOADER_MIDDLEWARES = {
        'scrapy_splash.SplashCookiesMiddleware': 723,
        'scrapy_splash.SplashMiddleware': 725,
        'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810,
    }
    DUPEFILTER_CLASS = 'scrapy_splash.SplashAwareDupeFilter'
    
  • 示例代码:
    from scrapy_splash import SplashRequest
    
    class ExampleSpider(scrapy.Spider):
        name = 'example'
        start_urls = ['http://example.com']
    
        def start_requests(self):
            for url in self.start_urls:
                yield SplashRequest(url, self.parse, args={'wait': 3})
    
        def parse(self, response):
            self.log(response.text)
    

Playwright:

  • 安装:

    pip install scrapy-playwright

  • 示例代码:
    class ExampleSpider(scrapy.Spider):
        name = 'example'
    
        def start_requests(self):
            yield scrapy.Request(
                url='http://example.com',
                meta={'playwright': True}
            )
    
        def parse(self, response):
            self.log(response.text)
    

    8. 反爬策略与解决方法
  • 设置 User-Agent:settings.py 中添加:

    USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'

  • 处理 Cookies: Scrapy 支持自动管理 Cookies,也可以手动设置:

    yield scrapy.Request(url, cookies={'key': 'value'})

  • 请求头伪装:

    headers = {

        'User-Agent': 'Your User-Agent',

        'Referer': 'http://example.com'

    }

    yield scrapy.Request(url, headers=headers)

  • 降低爬取速度:

    DOWNLOAD_DELAY = 2


9. 进阶技巧
  • 多层解析: 如果页面需要多次请求:

    def parse(self, response): 
        links = response.xpath('//a/@href').getall() 
        for link in links: 
            yield response.follow(link, self.parse_detail) 
    def parse_detail(self, response): 
        self.log(response.url)
  • 使用代理:

    PROXY = 'http://your_proxy'

    yield scrapy.Request(url, meta={'proxy': PROXY})

  • 分布式爬取: 使用 Scrapy + Redis 实现分布式爬虫。


10. 调试与测试
  • 调试 XPath 或 CSS: 在浏览器开发者工具的 Console 中测试:

    $x('//h1/text()') // XPath

    document.querySelectorAll('h1') // CSS

  • Scrapy Shell: 启动交互式调试:

    scrapy shell 'http://example.com'

11. 总结

Scrapy 是一个非常强大的爬虫框架,适合从简单的静态页面到复杂的动态加载内容的抓取需求。通过不断优化爬虫的结构和策略,可以更高效地完成数据采集任务。

  • 多层解析: 如果页面需要多次请求:

    def parse(self, response):
        links = response.xpath('//a/@href').getall()
        for link in links:
            yield response.follow(link, self.parse_detail)
    
    def parse_detail(self, response):
        self.log(response.url)
    
  • 使用代理:

    PROXY = 'http://your_proxy'

    yield scrapy.Request(url, meta={'proxy': PROXY})

  • 分布式爬取: 使用 Scrapy + Redis 实现分布式爬虫。

  • 调试 XPath 或 CSS: 在浏览器开发者工具的 Console 中测试:

    // XPath document.querySelectorAll('h1') // CSS

    $x('//h1/text()')

  • Scrapy Shell: 启动交互式调试:

    scrapy shell 'http://example.com'


网站公告

今日签到

点亮在社区的每一天
去签到