当前位置: 首页 > article >正文

scrapy 爬虫学习之【中医方剂】爬虫

本项目纯学习使用。

1 scrapy 代码

爬取逻辑非常简单,根据url来处理翻页,然后获取到详情页面的链接,再去爬取详情页面的内容即可!
需要注意的是:这里面有一个方剂多个来源的情况,这个没有处理。
最终数据落地到excel中。
经测试,总计获取 24909条中医方剂数据。

import pandas as pd
import scrapy


class ZhongyaofangSpider(scrapy.Spider):
    name = 'fangji'
    allowed_domains = ['zysj.com.cn']
    start_urls = [f"https://www.zysj.com.cn/zhongyaofang/index_{i}.html" for i in range(1, 27)]

    def __init__(self, *args, **kwargs):
        self.data = []

    def parse(self, response):
        # 提取包含链接和标题的部分
        for li in response.css('div#list-content ul li'):
            a_tag = li.css('a')
            title = a_tag.css('::attr(title)').get()
            href = a_tag.css('::attr(href)').get()
            if title and href:
                # 构建完整的详情页 URL
                detail_url = response.urljoin(href)
                yield scrapy.Request(detail_url, callback=self.parse_detail, meta={'title': title})

    # 如果没有这个元素,就设置空就行
    # 处方: div[class="item prescription"]/div[class="item-content"]/p/text
    # 制法: div[ class="item making"]/div[class="item-content"]/p/text
    # 主治: div class="item functional_indications"]/div[class="item-content"]/p/text
    # 用法用量: class="item usage"]/div[class="item-content"]/p/text
    # 注意事项: class="item care"]/div[class="item-content"]/p/text
    # 摘录: class="item excerpt"]/div[class="item-content"]/text
    def parse_detail(self, response):
        title = response.meta['title']

        # 提取各个字段
        prescription = response.css('div.item.prescription div.item-content p::text').get(default='').strip()
        making = response.css('div.item.making div.item-content p::text').get(default='').strip()
        functional_indications = response.css('div.item.functional_indications div.item-content p::text').get(
            default='').strip()
        usage = response.css('div.item.usage div.item-content p::text').get(default='').strip()
        care = response.css('div.item.care div.item-content p::text').get(default='').strip()
        excerpt = response.css('div.item.excerpt div.item-content::text').get(default='').strip()

        item = {
            'title': title,
            'prescription': prescription,
            'making': making,
            'functional_indications': functional_indications,
            'usage': usage,
            'care': care,
            'excerpt': excerpt
        }

        self.data.append(item)

        yield item

    def closed(self, reason):
        # 当爬虫关闭时,保存数据到 Excel 文件
        df = pd.DataFrame(self.data)
        df.to_excel('zhongyaofang_data.xlsx', index=False)

2 爬取截图

在这里插入图片描述

爬取后的数据

在这里插入图片描述


http://www.kler.cn/news/359096.html

相关文章:

  • 本地装了个pytorch cuda
  • YOLO元年!目标检测最强模型YOLOV11发布,全网首发yolov11原理+实战+论文解读教程!通俗易懂,科研人连夜水一篇SCI论文!计算机视觉|CV
  • 完整发布/上传uniapp Ios应用到App Store流程
  • CAN上位机软件VBDSP的控件如何实现按钮一次发送多条报文(自动化测试)
  • uniapp-uniapp + vue3 + pinia 搭建uniapp模板
  • 【前端】Matter:过滤与高级碰撞检测
  • 解决IntelliJ IDEA启动失败的完整指南
  • 世界数字农业盛宴与技术探索,25年3月聚焦世界灌溉科技大会
  • rootless模式下测试istio Ambient功能
  • Windows下的快速精准的抠图算法(Python实现,内含代码下载链接)
  • git restore恢复删除文件
  • 协议 MQTT
  • Scala中抽象类重写
  • 使用JMeter录制元件来录制HTTPS下的脚本
  • 第三方软件检测机构有哪些服务?重庆软件检测机构分享
  • C# WinForm实现画笔签名及解决MemoryBmp格式问题
  • YOLOv11改进策略【Conv和Transformer】| ACmix 卷积和自注意力的结合,充分发挥两者优势
  • 【MATLAB源码-第278期】基于matlab的ACO-OFDM系统仿真,输出误码率曲线图、时域频域图和子载波分离时域图。
  • Python内置函数classmethod()详解
  • JAVA设计模式学习之策略模式详解