python爬取百度搜索图片

,开发笔记,插件代码, 1684 浏览 2024-03-05 发布

python爬取百度搜索图片

python爬取百度搜索图片

效果:

代码如下

# -*- coding:utf8 -*-

import requests

import json

from urllib import parse

import os

import time

 

class BaiduImageSpider(object):

    def __init__(self):

        self.json_count = 0  # 请求到的json文件数量(一个json文件包含30个图像文件)

        self.url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&logid=5179920884740494226&ipn=rj&ct' \

                   '=201326592&is=&fp=result&queryWord={' \

                   '}&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=&latest=&copyright=&word={' \

                   '}&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&nojc=&pn={' \

                   '}&rn=30&gsm=1e&1635054081427= '

        self.directory = r"C:\Users\cun\Pictures\download\{}"  # 存储目录  这里需要修改为自己希望保存的目录  {}不要丢

        self.header = {

            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '

                          'Chrome/95.0.4638.54 Safari/537.36 Edg/95.0.1020.30 '

        }

 

    # 创建存储文件夹

    def create_directory(self, name):

        self.directory = self.directory.format(name)

        # 如果目录不存在则创建

        if not os.path.exists(self.directory):

            os.makedirs(self.directory)

        self.directory += r'\{}'

 

    # 获取图像链接

    def get_image_link(self, url):

        list_image_link = []

        strhtml = requests.get(url, headers=self.header)  # Get方式获取网页数据

        jsonInfo = json.loads(strhtml.text)

        for index in range(30):

            list_image_link.append(jsonInfo['data'][index]['thumbURL'])

        return list_image_link

 

    # 下载图片

    def save_image(self, img_link, filename):

        res = requests.get(img_link, headers=self.header)

        if res.status_code == 404:

            print(f"图片{img_link}下载出错------->")

        with open(filename, "wb") as f:

            f.write(res.content)

            print("存储路径:" + filename)

 

    # 入口函数

    def run(self):

        searchName = input("查询内容:")

        searchName_parse = parse.quote(searchName)  # 编码

 

        self.create_directory(searchName)

 

        pic_number = 0  # 图像数量

        for index in range(self.json_count):

            pn = (index+1)*30

            request_url = self.url.format(searchName_parse, searchName_parse, str(pn))

            list_image_link = self.get_image_link(request_url)

            for link in list_image_link:

                pic_number += 1

                self.save_image(link, self.directory.format(str(pic_number)+'.jpg'))

                time.sleep(0.2)  # 休眠0.2秒,防止封ip

        print(searchName+"----图像下载完成--------->")

 

if __name__ == '__main__':

    spider = BaiduImageSpider()

    spider.json_count = 10   # 定义下载10组图像,也就是三百张

    spider.run()


 

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-09-18 17:25:39 HTTP/2.0 GET : https://www.zuoyewo.com/help/55
  2. 运行时间 : 0.081212s [ 吞吐率:12.31req/s ] 内存消耗:4,161.37kb 文件加载:141
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=4aebea70e3c45fe020dff791a2e7bbdd
  1. /www/wwwroot/zuoyewo.com/public/index.php ( 1.73 KB )
  2. /www/wwwroot/zuoyewo.com/vendor/autoload.php ( 0.75 KB )
  3. /www/wwwroot/zuoyewo.com/vendor/composer/autoload_real.php ( 1.63 KB )
  4. /www/wwwroot/zuoyewo.com/vendor/composer/platform_check.php ( 0.90 KB )
  5. /www/wwwroot/zuoyewo.com/vendor/composer/ClassLoader.php ( 15.99 KB )
  6. /www/wwwroot/zuoyewo.com/vendor/composer/autoload_static.php ( 4.56 KB )
  7. /www/wwwroot/zuoyewo.com/vendor/topthink/think-helper/src/helper.php ( 8.34 KB )
  8. /www/wwwroot/zuoyewo.com/vendor/topthink/think-validate/src/helper.php ( 2.19 KB )
  9. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/helper.php ( 1.47 KB )
  10. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/stubs/load_stubs.php ( 0.16 KB )
  11. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Exception.php ( 1.69 KB )
  12. /www/wwwroot/zuoyewo.com/vendor/topthink/think-container/src/Facade.php ( 2.71 KB )
  13. /www/wwwroot/zuoyewo.com/vendor/symfony/polyfill-mbstring/bootstrap.php ( 8.26 KB )
  14. /www/wwwroot/zuoyewo.com/vendor/symfony/polyfill-mbstring/bootstrap80.php ( 9.78 KB )
  15. /www/wwwroot/zuoyewo.com/vendor/symfony/var-dumper/Resources/functions/dump.php ( 0.99 KB )
  16. /www/wwwroot/zuoyewo.com/vendor/topthink/think-dumper/src/helper.php ( 0.18 KB )
  17. /www/wwwroot/zuoyewo.com/vendor/symfony/var-dumper/VarDumper.php ( 3.73 KB )
  18. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/App.php ( 15.00 KB )
  19. /www/wwwroot/zuoyewo.com/vendor/topthink/think-container/src/Container.php ( 15.62 KB )
  20. /www/wwwroot/zuoyewo.com/vendor/psr/container/src/ContainerInterface.php ( 1.02 KB )
  21. /www/wwwroot/zuoyewo.com/app/provider.php ( 0.19 KB )
  22. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Http.php ( 6.04 KB )
  23. /www/wwwroot/zuoyewo.com/vendor/topthink/think-helper/src/helper/Str.php ( 7.29 KB )
  24. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Env.php ( 4.68 KB )
  25. /www/wwwroot/zuoyewo.com/app/common.php ( 0.03 KB )
  26. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/helper.php ( 18.78 KB )
  27. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Config.php ( 5.54 KB )
  28. /www/wwwroot/zuoyewo.com/config/admin.php ( 0.14 KB )
  29. /www/wwwroot/zuoyewo.com/config/app.php ( 0.95 KB )
  30. /www/wwwroot/zuoyewo.com/config/cache.php ( 0.81 KB )
  31. /www/wwwroot/zuoyewo.com/config/console.php ( 0.23 KB )
  32. /www/wwwroot/zuoyewo.com/config/cookie.php ( 0.56 KB )
  33. /www/wwwroot/zuoyewo.com/config/database.php ( 2.18 KB )
  34. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/facade/Env.php ( 1.67 KB )
  35. /www/wwwroot/zuoyewo.com/config/filesystem.php ( 0.61 KB )
  36. /www/wwwroot/zuoyewo.com/config/frontend.php ( 0.21 KB )
  37. /www/wwwroot/zuoyewo.com/config/lang.php ( 0.91 KB )
  38. /www/wwwroot/zuoyewo.com/config/log.php ( 1.35 KB )
  39. /www/wwwroot/zuoyewo.com/config/middleware.php ( 0.19 KB )
  40. /www/wwwroot/zuoyewo.com/config/route.php ( 1.89 KB )
  41. /www/wwwroot/zuoyewo.com/config/session.php ( 0.57 KB )
  42. /www/wwwroot/zuoyewo.com/config/trace.php ( 0.34 KB )
  43. /www/wwwroot/zuoyewo.com/config/view.php ( 0.82 KB )
  44. /www/wwwroot/zuoyewo.com/app/event.php ( 0.25 KB )
  45. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Event.php ( 7.67 KB )
  46. /www/wwwroot/zuoyewo.com/app/service.php ( 0.13 KB )
  47. /www/wwwroot/zuoyewo.com/app/AppService.php ( 0.26 KB )
  48. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Service.php ( 1.64 KB )
  49. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Lang.php ( 7.35 KB )
  50. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/lang/zh-cn.php ( 13.70 KB )
  51. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/initializer/Error.php ( 3.31 KB )
  52. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/initializer/RegisterService.php ( 1.33 KB )
  53. /www/wwwroot/zuoyewo.com/vendor/services.php ( 0.15 KB )
  54. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/service/PaginatorService.php ( 1.52 KB )
  55. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/service/ValidateService.php ( 0.99 KB )
  56. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/service/ModelService.php ( 1.75 KB )
  57. /www/wwwroot/zuoyewo.com/vendor/topthink/think-trace/src/Service.php ( 0.77 KB )
  58. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Middleware.php ( 6.72 KB )
  59. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/initializer/BootService.php ( 0.77 KB )
  60. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/Paginator.php ( 11.86 KB )
  61. /www/wwwroot/zuoyewo.com/vendor/topthink/think-validate/src/Validate.php ( 63.20 KB )
  62. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/Model.php ( 23.55 KB )
  63. /www/wwwroot/zuoyewo.com/vendor/topthink/think-helper/src/contract/Arrayable.php ( 0.09 KB )
  64. /www/wwwroot/zuoyewo.com/vendor/topthink/think-helper/src/contract/Jsonable.php ( 0.13 KB )
  65. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/model/contract/Modelable.php ( 0.09 KB )
  66. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/model/concern/Attribute.php ( 21.05 KB )
  67. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/model/concern/AutoWriteData.php ( 4.21 KB )
  68. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/model/concern/Conversion.php ( 6.44 KB )
  69. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/model/concern/DbConnect.php ( 5.16 KB )
  70. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/model/concern/ModelEvent.php ( 2.33 KB )
  71. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/model/concern/RelationShip.php ( 28.29 KB )
  72. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Db.php ( 2.88 KB )
  73. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/DbManager.php ( 8.52 KB )
  74. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Log.php ( 6.28 KB )
  75. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Manager.php ( 3.92 KB )
  76. /www/wwwroot/zuoyewo.com/vendor/psr/log/src/LoggerInterface.php ( 2.68 KB )
  77. /www/wwwroot/zuoyewo.com/vendor/psr/log/src/LoggerTrait.php ( 2.69 KB )
  78. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Cache.php ( 4.92 KB )
  79. /www/wwwroot/zuoyewo.com/vendor/psr/simple-cache/src/CacheInterface.php ( 4.71 KB )
  80. /www/wwwroot/zuoyewo.com/vendor/topthink/think-helper/src/helper/Arr.php ( 16.63 KB )
  81. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/cache/driver/File.php ( 7.84 KB )
  82. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/cache/Driver.php ( 9.03 KB )
  83. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/contract/CacheHandlerInterface.php ( 1.99 KB )
  84. /www/wwwroot/zuoyewo.com/app/Request.php ( 0.09 KB )
  85. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Request.php ( 55.74 KB )
  86. /www/wwwroot/zuoyewo.com/app/middleware.php ( 0.25 KB )
  87. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Pipeline.php ( 2.61 KB )
  88. /www/wwwroot/zuoyewo.com/vendor/topthink/think-trace/src/TraceDebug.php ( 3.40 KB )
  89. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/middleware/SessionInit.php ( 1.94 KB )
  90. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Session.php ( 1.80 KB )
  91. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/session/driver/File.php ( 6.27 KB )
  92. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/contract/SessionHandlerInterface.php ( 0.87 KB )
  93. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/session/Store.php ( 7.12 KB )
  94. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Route.php ( 23.56 KB )
  95. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/route/RuleName.php ( 5.34 KB )
  96. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/route/Domain.php ( 1.25 KB )
  97. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/route/RuleGroup.php ( 20.45 KB )
  98. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/route/Rule.php ( 26.35 KB )
  99. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/route/RuleItem.php ( 9.78 KB )
  100. /www/wwwroot/zuoyewo.com/route/app.php ( 6.09 KB )
  101. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/facade/Route.php ( 4.68 KB )
  102. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/facade/Config.php ( 1.37 KB )
  103. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/route/dispatch/Callback.php ( 2.37 KB )
  104. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/route/Dispatch.php ( 10.54 KB )
  105. /www/wwwroot/zuoyewo.com/app/Home/controller/Article.php ( 0.76 KB )
  106. /www/wwwroot/zuoyewo.com/app/Home/controller/Base.php ( 34.60 KB )
  107. /www/wwwroot/zuoyewo.com/app/BaseController.php ( 2.05 KB )
  108. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/facade/Session.php ( 1.79 KB )
  109. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/facade/Cookie.php ( 1.48 KB )
  110. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Cookie.php ( 6.06 KB )
  111. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/facade/Db.php ( 0.93 KB )
  112. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/connector/Mysql.php ( 5.44 KB )
  113. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/PDOConnection.php ( 52.47 KB )
  114. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/Connection.php ( 8.39 KB )
  115. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/ConnectionInterface.php ( 4.57 KB )
  116. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/builder/Mysql.php ( 16.58 KB )
  117. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/Builder.php ( 24.06 KB )
  118. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/BaseBuilder.php ( 27.50 KB )
  119. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/Query.php ( 15.71 KB )
  120. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/BaseQuery.php ( 45.13 KB )
  121. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/concern/TimeFieldQuery.php ( 7.43 KB )
  122. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/concern/AggregateQuery.php ( 3.26 KB )
  123. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php ( 20.07 KB )
  124. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/concern/ParamsBind.php ( 3.66 KB )
  125. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/concern/ResultOperation.php ( 7.01 KB )
  126. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/concern/WhereQuery.php ( 19.37 KB )
  127. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/concern/JoinAndViewQuery.php ( 7.11 KB )
  128. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php ( 2.63 KB )
  129. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/concern/Transaction.php ( 2.77 KB )
  130. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/log/driver/File.php ( 6.19 KB )
  131. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/contract/LogHandlerInterface.php ( 0.86 KB )
  132. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/log/Channel.php ( 3.88 KB )
  133. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/event/LogRecord.php ( 0.86 KB )
  134. /www/wwwroot/zuoyewo.com/vendor/topthink/think-orm/src/db/Express.php ( 1.53 KB )
  135. /www/wwwroot/zuoyewo.com/vendor/topthink/think-helper/src/Collection.php ( 16.47 KB )
  136. /www/wwwroot/zuoyewo.com/view/index/help_detail.php ( 2.00 KB )
  137. /www/wwwroot/zuoyewo.com/view/index/_header.php ( 1.83 KB )
  138. /www/wwwroot/zuoyewo.com/view/index/_footer.php ( 0.36 KB )
  139. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/Response.php ( 8.81 KB )
  140. /www/wwwroot/zuoyewo.com/vendor/topthink/framework/src/think/response/Html.php ( 0.97 KB )
  141. /www/wwwroot/zuoyewo.com/vendor/topthink/think-trace/src/Html.php ( 4.42 KB )
  1. CONNECT:[ UseTime:0.002057s ] mysql:host=rm-bp1tlcx10r742d0ik7o.mysql.rds.aliyuncs.com;port=3306;dbname=zyw;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `data_news_item` [ RunTime:0.001906s ]
  3. SELECT * FROM `data_news_item` WHERE `id` = '55' AND `status` = 1 AND `deleted` = 0 LIMIT 1 [ RunTime:0.001027s ]
  4. UPDATE `data_news_item` SET `num_read` = `num_read` + 1 WHERE `id` = '55' [ RunTime:0.001922s ]
  5. SELECT `id`,`name`,`remark`,`mark`,`num_read`,`create_at` FROM `data_news_item` WHERE `status` = 1 AND `deleted` = 0 ORDER BY `sort` DESC,`id` DESC LIMIT 8 [ RunTime:0.001409s ]
  6. SHOW FULL COLUMNS FROM `system_config` [ RunTime:0.001746s ]
  7. SELECT * FROM `system_config` WHERE `type` = 'base' [ RunTime:0.000676s ]
0.082825s