Scrapy爬虫在新闻数据提取中的应用

Scrapy是一个强大的爬虫框架,广泛用于从网站上提取结构化数据。下面这段代码是Scrapy爬虫的一个例子,用于从新闻网站上提取和分组新闻数据。

使用场景

在新闻分析和内容聚合的场景中,收集和组织新闻数据是常见需求。例如,如果我们需要为用户提供按日期分类的新闻更新,或者我们想分析特定时间段内的新闻趋势,这段代码就非常适合。

页面截图

在这里插入图片描述

结构截图

在这里插入图片描述

代码注释解释
# Scrapy爬虫的parse方法,用于处理响应并提取信息
def parse(self, resp, **kwargs):
    grouped_news_items = []  # 存储所有分组的新闻条目

    children = resp.xpath('//div[@class="news-list"]/*')  # 获取新闻列表中的所有子元素
    current_group = []  # 当前日期下的新闻条目集合
    current_date = None  # 当前新闻条目的日期

    # 遍历新闻列表中的每个子元素
    for child in children:
        # 如果子元素是日期标签,更新current_date并将之前的新闻组添加到grouped_news_items
        if 'news-date' in child.xpath('@class').get(''):
            if current_group:
                grouped_news_items.append((current_date, current_group))
                current_group = []
            current_date = child.xpath('normalize-space(text())').get()
        # 如果子元素是新闻条目,提取相关信息并添加到current_group
        elif 'news-item' in child.xpath('@class').get(''):
            news_info = {
   
                'title': child.xpath('./div/h2/a/text()').extract_first(),  # 新闻标题
                'link': child.xpath('./div/h2/a/@href').extract_first(),    # 新闻链接
                'source_name': child.xpath('./div/p/span/text()').extract()[1].strip(),  # 来源名称
                'source_img': child.xpath('./div/p/span/img/@data-src').extract_first()  # 来源图标
            }
            current_group.append(news_info)

    # 将最后一个日期的新闻条目集合添加到grouped_news_items
    if current_group:
        grouped_news_items.append((current_date, current_group))

    # 生成Scrapy Item,并通过yield返回
    for date, items in grouped_news_items:
        for item in items:
            an = AiNewsItem()  # Scrapy Item对象,用于存储新闻信息
            an['time_str'] = date
            an['title'] = item['title']
            an['source_name'] = item['source_name']
            an['source_img'] = item['source_img']
            an['link'] = item['link']
            yield an

相关推荐

  1. 爬虫框架Scrapy应用

    2024-01-25 22:30:02       35 阅读
  2. 爬虫Scrapy配置随机User-Agent中间件

    2024-01-25 22:30:02       89 阅读
  3. Scrapycrawlspider爬虫

    2024-01-25 22:30:02       57 阅读
  4. scrapycrawlspider爬虫

    2024-01-25 22:30:02       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-25 22:30:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-25 22:30:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-25 22:30:02       82 阅读
  4. Python语言-面向对象

    2024-01-25 22:30:02       91 阅读

热门阅读

  1. 基于智能化安全编排的网络安全事件响应架构

    2024-01-25 22:30:02       58 阅读
  2. 源码篇--Redisson 分布式锁lock的实现

    2024-01-25 22:30:02       48 阅读
  3. Spring复习--2024.1/26更新

    2024-01-25 22:30:02       62 阅读
  4. 在vim中对光标选中单词进行搜索

    2024-01-25 22:30:02       49 阅读
  5. Springboot自定义全局异常处理

    2024-01-25 22:30:02       61 阅读
  6. 华纳云:如何搭建一个简易的文件服务器?

    2024-01-25 22:30:02       52 阅读
  7. 【链表】-Lc21-合并两个有序链表(同时遍历)

    2024-01-25 22:30:02       51 阅读
  8. 请求优化--利用webpack实现根据路由进行懒加载

    2024-01-25 22:30:02       55 阅读