当前位置:   article > 正文

爬取百度图片_spider = baiduimagespider()

spider = baiduimagespider()

示例代码:

  1. import requests
  2. import os
  3. import urllib
  4. class Spider_baidu_image():
  5. def __init__(self):
  6. self.url = 'http://image.baidu.com/search/acjson?'
  7. self.headers = {
  8. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.\
  9. 3497.81 Safari/537.36'}
  10. self.headers_image = {
  11. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.\
  12. 3497.81 Safari/537.36',
  13. 'Referer': 'http://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1557124645631_R&pv=&ic=&nc=1&z=&hd=1&latest=0&copyright=0&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&sid=&word=%E8%83%A1%E6%AD%8C'}
  14. # self.keyword = '小猫'
  15. self.keyword = input("请输入搜索图片关键字:")
  16. self.paginator = int(input("请输入搜索页数,每页30张图片:"))
  17. # self.paginator = 50
  18. # print(type(self.keyword),self.paginator)
  19. def get_param(self):
  20. """
  21. 获取url请求的参数,存入列表并返回
  22. :return:
  23. """
  24. keyword = urllib.parse.quote(self.keyword)
  25. params = []
  26. for i in range(1, self.paginator + 1):
  27. params.append(
  28. 'tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord={}&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=&hd=1&latest=0&copyright=0&word={}&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&cg=star&pn={}&rn=30&gsm=78&1557125391211='.format(
  29. keyword, keyword, 30 * i))
  30. return params
  31. def get_urls(self, params):
  32. """
  33. 由url参数返回各个url拼接后的响应,存入列表并返回
  34. :return:
  35. """
  36. urls = []
  37. for i in params:
  38. urls.append(self.url + i)
  39. return urls
  40. def get_image_url(self, urls):
  41. image_url = []
  42. for url in urls:
  43. json_data = requests.get(url, headers=self.headers).json()
  44. json_data = json_data.get('data')
  45. for i in json_data:
  46. if i:
  47. image_url.append(i.get('thumbURL'))
  48. return image_url
  49. def get_image(self, image_url):
  50. """
  51. 根据图片url,在本地目录下新建一个以搜索关键字命名的文件夹,然后将每一个图片存入。
  52. :param image_url:
  53. :return:
  54. """
  55. cwd = os.getcwd()
  56. file_name = os.path.join(cwd, self.keyword)
  57. if not os.path.exists(self.keyword):
  58. os.mkdir(file_name)
  59. for index, url in enumerate(image_url, start=1):
  60. with open(file_name + '\\{}.jpg'.format(index), 'wb') as f:
  61. f.write(requests.get(url, headers=self.headers_image).content)
  62. if index != 0 and index % 30 == 0:
  63. print('{}第{}页下载完成'.format(self.keyword, index / 30))
  64. def __call__(self, *args, **kwargs):
  65. params = self.get_param()
  66. urls = self.get_urls(params)
  67. image_url = self.get_image_url(urls)
  68. self.get_image(image_url)
  69. if __name__ == '__main__':
  70. spider = Spider_baidu_image()
  71. spider()

运行结果:

 图片输出效果:

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

闽ICP备14008679号