码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Scrapy案例(一)


    目录

    1. 创建项⽬

    2. 创建Spider

    3. 创建Item

    4. Spider

    5.保存数据

        1. 命令保存(⽂件:csv ,json ...)

        2.管道保存

    完整代码

    目录结构

    spider.py

    items.py

    pipelines.py

    setting.py

    run.py


    • ⽬标⽹站:https://quotes.toscrape.com/
    • 数据需求:
    1. 名⾔
    2.  名⼈
    3. 标签
    • 分析⽹站: 数据静态加载

    1. 创建项⽬

    scrapy startproject my_scrapy # 项⽬名

    2. 创建Spider

    cd my_scrapy
    scrapy genspider spider baidu . com # 爬⾍⽂件名 域名

    3. 创建Item

    Item是保存数据的容器 定义爬取数据结构

    4. Spider

    • 定义初始请求
    • 定义解析数据

    5.保存数据

        1. 命令保存(⽂件:csv ,json ...)

                    scrapy crawl spider -o demo.csv

        2.管道保存

    1. class MyScrapyPipeline:
    2. def process_item(self, item, spider):
    3. # 简单保存
    4. with open('demo2.txt','a',encoding= 'utf8') as f:
    5. f.write(item['author'] + '\n'+ item['text'] + '\n\n\n')
    6. return item

    完整代码

    目录结构

    spider.py

    1. import scrapy
    2. from my_scrapy.items import MyScrapyItem
    3. class SpiderSpider(scrapy.Spider):
    4. # 爬虫名称
    5. name = 'spider'
    6. # 域名限制,允许爬取的范围
    7. # allowed_domains = ['https://quotes.toscrape.com/']
    8. # 初始请求的页面
    9. start_urls = ['https://quotes.toscrape.com//']
    10. def parse(self, response):
    11. # text = response.text
    12. quotes = response.xpath('//div[@class="quote"]')
    13. for quote in quotes :
    14. # 旧方法 get()为新方法
    15. # text = quote.xpath('./span[@class = "text"]/text()').extract_first()
    16. # 实例化对象
    17. item = MyScrapyItem()
    18. # 利用xpth进行爬取
    19. text = quote.xpath('./span[@class = "text"]/text()').get()
    20. author = quote.xpath('.//small[@class="author"]/text()').get()
    21. Tags = quote.xpath('.//a[@class="tag"]/text()').getall()
    22. item['text'] = text
    23. item['author'] = author
    24. item['Tag'] = Tags
    25. # 迭代出去
    26. yield item

    items.py

    1. import scrapy
    2. class MyScrapyItem(scrapy.Item):
    3. # define the fields for your item here like:
    4. # name = scrapy.Field()
    5. # 名言
    6. text = scrapy.Field()
    7. # 名人
    8. author = scrapy.Field()
    9. # 标签
    10. Tag = scrapy.Field()

    pipelines.py

    1. class MyScrapyPipeline:
    2. def process_item(self, item, spider):
    3. # 简单保存
    4. with open('demo2.txt','a',encoding= 'utf8') as f:
    5. f.write(item['author'] + '\n'+ item['text'] + '\n\n\n')
    6. return item

    setting.py

    1. ITEM_PIPELINES = {
    2. 'my_scrapy.pipelines.MyScrapyPipeline': 300,
    3. }

    run.py

    1. from scrapy import cmdline
    2. cmdline.execute('scrapy crawl spider '.split())

  • 相关阅读:
    如何防止用户重复提交订单?(上)
    http请求方式及传参方式
    3.5背景图像固定(背景附着)
    Unity EmbeddedBrowser浏览器插件事件通讯
    Tomcat
    【AAAI2023】视觉辅助的常识知识获取Visually Grounded Commonsense Knowledge Acquisition 个人学习笔记
    我的大模型岗位面试总结:共24家,9个offer
    Web3.0代币将如何颠覆传统内容创作模式?
    【CVPR2022】Detecting Camouflaged Object in Frequency Domain
    《痞子衡嵌入式半月刊》 第 103 期
  • 原文地址:https://blog.csdn.net/qq_51179608/article/details/125492782
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号