• scrapy基本使用


    直接去cmd下载

     下载好之后打开pycharm

    在命令行里面创建新项目

    这个demo01是项目的名字,这个可以随便写的。 然后回车

    这样就是创建好了。可以cd到那个项目,但是这里直接在file里面打开就行了!

    file-open-demo1 

     点ok然后点this windows,就是在当前窗口打开,关掉之后再打开还是这个项目,如果点new windows的话就是新窗口打开,重启后是之前的项目。

    这个是创建一个spider,

    scrapy genspider baidu baidu.com

     这里的第一个baidu是名字,然后跟着域名。创建完成后在spiders里面会发现多了一个baidu.py。然后我们进入这个py

    name就是名字,项目名,allowed_domains呢就是域名,start_urls是开始的地方,想从哪里开始爬就从这里修改就行了,下面的parse()是解析的 

    1. # Scrapy settings for demo01 project
    2. #
    3. # For simplicity, this file contains only settings considered important or
    4. # commonly used. You can find more settings consulting the documentation:
    5. #
    6. # https://docs.scrapy.org/en/latest/topics/settings.html
    7. # https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
    8. # https://docs.scrapy.org/en/latest/topics/spider-middleware.html
    9. BOT_NAME = 'demo01' # 爬虫项目名
    10. SPIDER_MODULES = ['demo01.spiders']
    11. NEWSPIDER_MODULE = 'demo01.spiders'
    12. # Crawl responsibly by identifying yourself (and your website) on the user-agent
    13. USER_AGENT = 'Mozlla/5.0' # user_agent这个可以改,可以在这里设置,也可以在下面设置
    14. # Obey robots.txt rules
    15. ROBOTSTXT_OBEY = False # 是否遵循robots协议,一定要设置为false
    16. # Configure maximum concurrent requests performed by Scrapy (default: 16)
    17. CONCURRENT_REQUESTS = 8 # 最大并发量,默认为16 这个要改
    18. # Configure a delay for requests for the same website (default: 0)
    19. # See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
    20. # See also autothrottle settings and docs
    21. DOWNLOAD_DELAY = 1 # 下载延迟请求,每隔多长时间发送一个请求(减低数据爬取频率)
    22. # The download delay setting will honor only one of:
    23. #CONCURRENT_REQUESTS_PER_DOMAIN = 16
    24. #CONCURRENT_REQUESTS_PER_IP = 16
    25. # Disable cookies (enabled by default)
    26. #COOKIES_ENABLED = False # 是否启用cookies,默认禁止,取消注释就是开启
    27. # Disable Telnet Console (enabled by default)
    28. #TELNETCONSOLE_ENABLED = False
    29. # Override the default request headers:
    30. # 请求头,类似于requests.get()方法中的headers参数
    31. DEFAULT_REQUEST_HEADERS = {
    32. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    33. 'Accept-Language': 'en',
    34. 'User-Agent': 'Mozilla/5.0'
    35. }
    36. # Enable or disable spider middlewares
    37. # See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
    38. #SPIDER_MIDDLEWARES = {
    39. # 'demo01.middlewares.Demo01SpiderMiddleware': 543,
    40. #}
    41. # Enable or disable downloader middlewares
    42. # See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
    43. #DOWNLOADER_MIDDLEWARES = {
    44. # 'demo01.middlewares.Demo01DownloaderMiddleware': 543,
    45. #}
    46. # Enable or disable extensions
    47. # See https://docs.scrapy.org/en/latest/topics/extensions.html
    48. #EXTENSIONS = {
    49. # 'scrapy.extensions.telnet.TelnetConsole': None,
    50. #}
    51. # Configure item pipelines
    52. # See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
    53. #ITEM_PIPELINES = {
    54. # 'demo01.pipelines.Demo01Pipeline': 300,
    55. #}
    56. # Enable and configure the AutoThrottle extension (disabled by default)
    57. # See https://docs.scrapy.org/en/latest/topics/autothrottle.html
    58. #AUTOTHROTTLE_ENABLED = True
    59. # The initial download delay
    60. #AUTOTHROTTLE_START_DELAY = 5
    61. # The maximum download delay to be set in case of high latencies
    62. #AUTOTHROTTLE_MAX_DELAY = 60
    63. # The average number of requests Scrapy should be sending in parallel to
    64. # each remote server
    65. #AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
    66. # Enable showing throttling stats for every response received:
    67. #AUTOTHROTTLE_DEBUG = False
    68. # Enable and configure HTTP caching (disabled by default)
    69. # See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
    70. #HTTPCACHE_ENABLED = True
    71. #HTTPCACHE_EXPIRATION_SECS = 0
    72. #HTTPCACHE_DIR = 'httpcache'
    73. #HTTPCACHE_IGNORE_HTTP_CODES = []
    74. #HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

    回到baidu.py

    然后在pycharm里面的终端运行项目

     

    上面是检查你的scrapy的配置,版本之类的东西,然后

     

     发现这个,提取出来是字典。

    在这里创建一个run.py出来

    输入

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

     然后右键运行

     

     

    简单明了,颜色都不一样,直接可以看到自己想要爬取的内容。

    回到刚刚的问题,

     

    然后运行一下

     

    接着修改

     

     

  • 相关阅读:
    luogu P5560 [Celeste-B]Golden Feather
    Python123 期末题库
    Linux 进程终止 进程等待 进程替换 简易shell
    Hadoop3:MapReduce中的Reduce Join和Map Join
    python基础:文件选择功能获取文件绝对地址
    【前端面试知识题】- 4.1 JavaScript
    新库上线 | CnOpenData招聘公司基本信息扩展数据
    统计信号处理基础 习题解答6-14
    Centos8界面语言怎么设置? Centos用户界面语言的设置方法
    基于Spring Boot+Vue的毕业设计选题管理系统设计与实现
  • 原文地址:https://blog.csdn.net/m0_66060262/article/details/126012246