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

作者:懒人笔记—001 时间:2021-07-15 00:22:34 

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


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('程序结束')

来源:https://blog.csdn.net/ZHANG_TIMI/article/details/102894401

标签:python3,图片下载器
0
投稿

猜你喜欢

  • Dreamweaver2004 中文乱码解决方案

    2007-01-31 10:20:00
  • 谈谈从phpinfo中能获取哪些值得注意的信息

    2023-11-23 23:52:17
  • 深入浅析Django MTV模式

    2021-08-18 07:55:32
  • 使用ODBC接口访问MySQL

    2009-02-13 13:59:00
  • python-yml文件读写与xml文件读写

    2022-06-16 06:43:50
  • IE下Flash内容刷新后消失问题

    2008-01-02 12:38:00
  • phpstorm断点调试方法图文详解

    2023-05-30 01:06:40
  • Python代理抓取并验证使用多线程实现

    2022-07-10 07:04:19
  • asp如何远程注册DLL

    2010-06-16 09:58:00
  • Oracle批量查询、删除、更新使用BULK COLLECT提高效率

    2023-07-14 03:15:52
  • on error goto (Vbscript)和try catch

    2008-08-04 13:22:00
  • python密码学RSA密码加密教程

    2023-08-03 17:09:10
  • Python ftp上传文件

    2023-10-01 06:35:34
  • 960网格系统

    2009-02-17 12:22:00
  • PHP操作MySQL中BLOB字段的方法示例【存储文本与图片】

    2023-11-23 23:45:27
  • python对csv文件追加写入列的方法

    2022-11-14 01:56:29
  • python 定时器,实现每天凌晨3点执行的方法

    2023-06-25 12:32:01
  • SQLserver中字符串查找功能patindex和charindex的区别

    2012-06-06 20:20:42
  • 怎样在SQL Server 2005中用证书加密数据

    2009-01-04 14:11:00
  • 贝聿铭写给年轻设计师的十点忠告

    2010-01-24 18:46:00
  • asp之家 网络编程 m.aspxhome.com