去评论
就爱江湖 Www.92Jh.Cn

通过关键词搜索下载百度图库图片

重现江湖
2020/01/05 19:06:41
下载之前要保证电脑拥有python环境哦python3下运行正常
源码代码:
  1. #www.92jh.cn

  2. import re
  3. import requests
  4. from urllib import error
  5. from bs4 import BeautifulSoup
  6. import os

  7. num = 0
  8. numPicture = 0
  9. file = ''
  10. List = []


  11. def Find(url):
  12.     global List
  13.     print('正在检测图片总数,请稍等.....')
  14.     t = 0
  15.     i = 1
  16.     s = 0
  17.     while t < 1000:
  18.         Url = url + str(t)
  19.         try:
  20.             Result = requests.get(Url, timeout=7)
  21.         except BaseException:
  22.             t = t + 60
  23.             continue
  24.         else:
  25.             result = Result.text
  26.             pic_url = re.findall('"objURL":"(.*?)",', result, re.S)  # 先利用正则表达式找到图片url
  27.             s += len(pic_url)
  28.             if len(pic_url) == 0:
  29.                 break
  30.             else:
  31.                 List.append(pic_url)
  32.                 t = t + 60
  33.     return s


  34. def recommend(url):
  35.     Re = []
  36.     try:
  37.         html = requests.get(url)
  38.     except error.HTTPError as e:
  39.         return
  40.     else:
  41.         html.encoding = 'utf-8'
  42.         bsObj = BeautifulSoup(html.text, 'html.parser')
  43.         div = bsObj.find('div', id='topRS')
  44.         if div is not None:
  45.             listA = div.findAll('a')
  46.             for i in listA:
  47.                 if i is not None:
  48.                     Re.append(i.get_text())
  49.         return Re


  50. def dowmloadPicture(html, keyword):
  51.     global num
  52.     # t =0
  53.     pic_url = re.findall('"objURL":"(.*?)",', html, re.S)  # 先利用正则表达式找到图片url
  54.     print('找到关键词:' + keyword + '的图片,即将开始下载图片...')
  55.     for each in pic_url:
  56.         print('正在下载第' + str(num + 1) + '张图片,图片地址:' + str(each))
  57.         try:
  58.             if each is not None:
  59.                 pic = requests.get(each, timeout=7)
  60.             else:
  61.                 continue
  62.         except BaseException:
  63.             print('错误,当前图片无法下载')
  64.             continue
  65.         else:
  66.             string = file + r'\\' + keyword + '_' + str(num) + '.jpg'
  67.             fp = open(string, 'wb')
  68.             fp.write(pic.content)
  69.             fp.close()
  70.             num += 1
  71.         if num >= numPicture:
  72.             return


  73. if __name__ == '__main__':  # 主函数入口
  74.     word = input("请输入搜索关键词(可以是人名,地名等): ")
  75.     # 比如百度图片上搜索:苹果
  76.     # add = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=%e8%8b%b9%e6%9e%9c&pn=120'
  77.     url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + word + '&pn='
  78.     tot = Find(url)
  79.     Recommend = recommend(url)  # 记录相关推荐
  80.     print('经过检测{}类图片共有{}张'.format(word, tot))
  81.     numPicture = int(input('请输入想要下载的图片数量: '))
  82.     file = input('请建立一个存储图片的文件夹,输入文件夹名称即可:')
  83.     y = os.path.exists(file)
  84.     if y == 1:
  85.         print('该文件已存在,请重新输入')
  86.         file = input('请建立一个存储图片的文件夹,)输入文件夹名称即可')
  87.         os.mkdir(file)
  88.     else:
  89.         os.mkdir(file)
  90.     t = 0
  91.     tmp = url
  92.     while t < numPicture:
  93.         try:
  94.             url = tmp + str(t)
  95.             result = requests.get(url, timeout=10)
  96.             print(url)
  97.         except error.HTTPError as e:
  98.             print('网络错误,请调整网络后重试')
  99.             t = t + 60
  100.         else:
  101.             dowmloadPicture(result.text, word)
  102.             t = t + 60

  103.     print('当前搜索结束,感谢使用')
  104.     print('猜你喜欢')
  105.     for re in Recommend:
  106.         print(re, end='  ')