欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

基于python3 的百度图片下载器的实现代码

时间:2021-03-05 12:42:42|栏目:Python代码|点击:

自己写了玩的一个小脚本,百度图片下载

import re
import os
import requests
import hashlib
 
 
def dowmloadPic(html, keyword):
  pic_url = re.findall('"objURL":"(.*?)",', html, re.S)
 
  if len(pic_url) < 1:
    return 1
 
  i = 0
  for each in pic_url:
    print(i + 1, end=',')
    md5Str = hashlib.md5(each.encode("utf-8")).hexdigest()
 
    # 抓去链接
    oneStr = md5Str + '  ' + keyword + '  ' + each + '\n'
    with open('downText.txt', 'a+') as f:
      f.write(oneStr)
 
    # 下载图片
    # try:
    #   pic = requests.get(each, timeout=10)
    # except requests.exceptions.ConnectionError:
    #   print('链接超时,跳过此操作')
    #   continue
    #
    # kz = os.path.splitext(each)[-1]
    # photo = + keyword + '_' + str(i) + kz
    #
    # with open(photo, 'wb') as f:
    #   f.write(pic.content)
    i += 1
 
  print('\n')
  return 0
 
 
if __name__ == '__main__':
 
  word = input('enter a key word:')
  page = input('enter the page:')
 
  page = int(page)
  page = 1 if page < 1 else page
 
  url = 'http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + word + '&ct=201965323&v=flip'
 
  p = 1
  while (p <= page):
    print(word + ',第[' + str(p) + ']页:')
 
    pn = (p - 1) * 20
    url = url + '&pn=' + str(pn)
    result = requests.get(url).content.decode('utf-8')
 
    code = dowmloadPic(result, word)
 
    if code:
      print('无相关数据,提前退出程序')
      break
    p = p + 1
 
  print('程序结束')

上一篇:深入解析Python的Tornado框架中内置的模板引擎

栏    目:Python代码

下一篇:Python读取一个目录下所有目录和文件的方法

本文标题:基于python3 的百度图片下载器的实现代码

本文地址:http://www.codeinn.net/misctech/74755.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有