当前位置:   article > 正文

python爬取图片(百度、搜狗、必应)_csdn爬虫爬取图片制作数据集

csdn爬虫爬取图片制作数据集

最近抽空学习了python爬虫,为了巩固知识,就顺手写了一个小程序,可以爬取百度、搜狗、必应这三大网站的图片。

程序放在github上,地址为:https://github.com/taojianglong/python-crawler

多线程爬取图片github地址为:https://github.com/taojianglong/multi_thread_crawler

下载程序后,解压crawler_spider.zip,直接运行文件夹中的crawler_img.py文件。然后根据自己需要下载图片。

该程序只是为了方便以后回顾,以及为需要此文件的人提供一点帮助,如有不足之处,请指正。

部分代码如下:

  1. import os
  2. import requests
  3. from baidu_img import baidu_pic_url
  4. from sougou_img import sougou_pic_url
  5. from biying_img import biying_pic_url
  6. class Crawler():
  7. def __init__(self, num, keyword, engine):
  8. self.num = num
  9. self.keyword = keyword
  10. self.engine = engine
  11. def down_img(self):
  12. if self.engine == 'baidu':
  13. pic_url = baidu_pic_url(self.num, self.keyword)
  14. elif self.engine == 'sougou':
  15. pic_url = sougou_pic_url(self.num, self.keyword)
  16. elif self.engine == 'biying':
  17. pic_url = biying_pic_url(self.num, self.keyword)
  18. else:
  19. print('错误的搜索引擎,请重新运行,输入:baidu、sougou、biying')
  20. if os.path.exists('D:/图片/' + self.engine + '/' + self.keyword):
  21. pass
  22. else:
  23. os.makedirs('D:/图片/' + self.engine + '/' + self.keyword)
  24. path = 'D:/图片/' + self.engine + '/'
  25. for index, i in enumerate(pic_url):
  26. try:
  27. filename = path + self.keyword + '/' + str(index) + '.jpg'
  28. print(filename)
  29. with open(filename, 'wb+') as f:
  30. f.write(requests.get(i).content)
  31. except:
  32. continue
  33. if __name__ == '__main__':
  34. keyword = str(input('请搜索关键词:(如:美女)'))
  35. num = int(input('请输入爬取图片数目:'))
  36. engine = str(input('请输入:baidu, sougou, biying其中之一'))
  37. crawler = Crawler(num, keyword, engine)
  38. crawler.down_img()

详细代码请移步github,下载完整代码。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/868458
推荐阅读
相关标签
  

闽ICP备14008679号