鱼C论坛

 找回密码
 立即注册
查看: 2531|回复: 2

[技术交流] 爬取360图片

[复制链接]
发表于 2019-7-22 15:14:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. from tkinter import *
  2. from tkinter import messagebox
  3. from tkinter import filedialog
  4. import tkinter.filedialog
  5. import urllib.request
  6. import urllib.parse
  7. import urllib.error
  8. import threading
  9. import inspect
  10. import ctypes
  11. import time
  12. import json
  13. import sys
  14. import os
  15. import re
  16. root = Tk()
  17. root.withdraw() #****实现主窗口隐藏
  18. root.update()
  19. lock = threading.Lock()
  20. k1=time.perf_counter()

  21. try:
  22.     # 开头语
  23.     k=time.strftime("%y-%m-%d  %H:%M:%S")
  24.     print("--------------------------------Python网络爬虫调用360图片网---------------------------------")
  25.     print("------当前时间:",k)

  26.     # 确定工作位置
  27.     b="0"
  28.     b=input("按Enter键确定工作位置(输入1默认当前位置)")
  29.     if b!="1":
  30.         #询问路径
  31.         b=str(tkinter.filedialog.askopenfilename())
  32.         weizhi=b
  33.         n=weizhi.count("/")
  34.         t=n-1
  35.         while True:
  36.             if n!=t:
  37.                 weizhi=weizhi[:-1]
  38.                 n=weizhi.count("/")
  39.             else:
  40.                 break
  41.     else:
  42.         weizhi=os.getcwd()
  43.     os.chdir(weizhi)
  44.     print("------工作位置已确定!",weizhi)

  45.     # 询问要搜索的内容
  46.     a=input("请输入要搜索的图片:")
  47.     print("------搜索词已确定!",a)

  48.     # 创建工作文件夹
  49.     b=a
  50.     n=0
  51.     while True:
  52.         try:
  53.             os.makedirs(weizhi+"\"+b)
  54.         except FileExistsError:
  55.             b=a
  56.             b+=str(n+1)
  57.             n+=1
  58.             continue
  59.         break
  60.     print("------工作文件夹已确定!",weizhi+"\"+b)
  61.     os.chdir(weizhi+"\"+b)
  62.     print("------工作位置已转移到:",weizhi+"\"+b)

  63.     # 编码搜索内容
  64.     a=a.encode('utf-8')
  65.     a=str(a)
  66.     a=a.replace("\\x","%")
  67.     a=a.upper()
  68.     a="%"+a[3:-1]
  69.     print("------搜索词已编码完成!",a)

  70.     # 设置网址
  71.     url='http://image.so.com/j?q='+a+'&src=srp&correct='+a+'&pn=60&ch=&sn=170&ps=168&pc=60&pd=1&prevsn=110&sid=91b72d98c21c1739643a4d14587f7db3&ran=0&ras=6&cn=0&gn=0&kn=50&comm=1'
  72.     print("------搜索网址已确认!",url)

  73.     # 设置head参数
  74.     head={}
  75.     head['User-Agent']="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
  76.     head = urllib.parse. urlencode(head).encode('utf-8' )
  77.     print("------head参数设置完成!",head)

  78.     # 访问网址
  79.     while True:
  80.         try:
  81.             print("------请求连接中......")
  82.             req=urllib.request.Request(url,head)
  83.             response = urllib.request.urlopen(req)
  84.         except urllib.error.URLError as reason:
  85.                 print("------连接发生错误!")
  86.                 print("------请检查您的网络连接!")
  87.                 print("------错误原因是:",reason)
  88.                 print("------按Enter键重新连接")
  89.                 input()
  90.                 continue
  91.         break
  92.     print("------网址已连接成功!")
  93.     html = response.read().decode('utf-8')
  94.     print("------网址内容获取完毕!")

  95.     # 内容分片
  96.     v=html.split("},{")
  97.     n=0
  98.     d=[]
  99.     print("------网址内容分片完成!")

  100.     # 查找地址
  101.     t=r'"img":"http:.{1,80}.jpg'
  102.     for i in v:
  103.         c=re.search(t,i)
  104.         if not c==None:
  105.             c=c.group()
  106.             c=c[7:]
  107.             c=c.replace("\\/\\/","//")
  108.             c=c.replace("\\/","/")
  109.             d.append(c)
  110.         else:
  111.             continue
  112.     print("------图片地址筛选完毕!")

  113.     # 搜索并下载
  114.     k2=time.perf_counter()
  115.     print("-------------------------------------进行文件下载任务--------------------------------------------")
  116.     print("------共有",len(d),"条下载任务")
  117.     print("----------------------------------------------------------------------------------------------------")
  118.     yichang=[]
  119.     yichang_num=[]

  120.     # 创建下载函数
  121.     def xiazai(name,url,number):
  122.         try:
  123.             response = urllib.request.urlopen(url)
  124.             cat_img = response.read()
  125.             with open(str(number)+'.jpg','wb') as f:
  126.                 f.write(cat_img)
  127.                 f.close()
  128.         except Exception as reason:
  129.             yichang.append(reason)
  130.             yichang_num.append(number)
  131.     print("------下载函数创建完毕!")

  132.     # 创建超时处理函数
  133.     def jishi():
  134.         # 挂起下载线程
  135.         fun_print("------下载线程正在挂起......")
  136.         n=0
  137.         for i in threads:
  138.             n+=1
  139.             i.setDaemon(True)
  140.             i.start()
  141.         fun_print("------"+str(n)+"条下载线程已全部挂起!")
  142.         # 倒数计时
  143.         time.sleep(50)
  144.         fun_print("------超时处理线程:还有10秒,清空下载线程!")
  145.         time.sleep(5)
  146.         fun_print("------超时处理线程:还有5秒,清空下载线程!")
  147.         time.sleep(2)
  148.         n=4
  149.         for i in range(3):
  150.             n-=1
  151.             fun_print("------超时处理线程:还有"+str(n)+"秒,清空下载线程!")
  152.             time.sleep(1)
  153.         # 清空线程
  154.         fun_print("------超时处理线程:开始清空下载线程!")
  155.         n=0
  156.         for i in threading.enumerate():
  157.             n+=1
  158.             if n>3:
  159.                 stop_thread(i)
  160.                 fun_print("------超时处理线程:清空了:"+str(i))
  161.         fun_print("------超时处理线程:下载线程清空完毕!")
  162.         fun_print("------超时处理线程:正在下线......")
  163.     print("------超时处理函数创建完毕!")

  164.     # 创建中止线程函数
  165.     def _async_raise(tid, exctype):
  166.         """raises the exception, performs cleanup if needed"""
  167.         tid = ctypes.c_long(tid)
  168.         if not inspect.isclass(exctype):
  169.             exctype = type(exctype)
  170.         res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
  171.         if res == 0:
  172.             raise ValueError("invalid thread id")
  173.         elif res != 1:
  174.             # """if it returns a number greater than one, you're in trouble,
  175.             # and you should call it again with exc=NULL to revert the effect"""
  176.             ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
  177.             raise SystemError("PyThreadState_SetAsyncExc failed")
  178.     def stop_thread(thread):
  179.         _async_raise(thread.ident, SystemExit)
  180.     print("------中止线程函数创建完毕!")

  181.     # 创建输出函数
  182.     def fun_print(name):
  183.         lock.acquire()
  184.         print(name)
  185.         lock.release()
  186.         
  187.     # 创建下载线程
  188.     threads = []
  189.     for i in range(len(d)):
  190.         threads.append(threading.Thread(target=xiazai,args=("线程"+str(i+1),d[i],i+1)))
  191.     print("------下载线程创建完毕!")
  192.             
  193.     # 主程序代码
  194.     # 挂起超时处理线程
  195.     if __name__ == '__main__':
  196.         t1=threading.Thread(target=jishi,args=(),name="超时处理线程")
  197.         fun_print("------超时处理线程创建完毕!")
  198.         t1.start()
  199.         fun_print("------超时处理线程已挂起!")
  200.         fun_print("------主进程在等待线程结束")
  201.         # 进度条读条
  202.         b=1000
  203.         while True:
  204.             a=threading.activeCount()
  205.             a=a-3
  206.             if a<b:
  207.                 fun_print("------汇报进度:下载进度:"+str(int(((len(d)-a)/len(d))*100))+"%, 剩余的下载线程数量:"+str(a))
  208.                 b=a
  209.             if a==0:
  210.                 # 关闭超时处理线程
  211.                 a=threading.enumerate()
  212.                 if len(a)==3:
  213.                     stop_thread(a[2])
  214.                 break
  215.         # 下载任务结束
  216.         print("------所有下载线程均已结束!")
  217.         k3=time.perf_counter()
  218.         print("------下载任务完成!")
  219.         print("----------------------------------------------------------------------------------------------------")
  220.         k1=k3-k1
  221.         k2=k3-k2
  222.         c='------程序总用时:{:.3f}秒\n------下载任务用时:{:.3f}秒'
  223.         print(c.format(k1,k2))
  224.         print("------成功下载",len(d)-len(yichang),"张图片")
  225.         print("------下载失败",len(yichang),"张图片")
  226.         print("------图片下载位置在:",os.getcwd())
  227.         os.chdir(weizhi)
  228.         print("------工作位置已移动到",weizhi)
  229.         print("----------------------------------------------------------------------------------------------------")
  230.         while True:
  231.             print("-------------------(输入'1'展开图片下载地址,'2'打开崩溃日志,'0'退出程序)-------------------")
  232.             a=input(">>>")
  233.             # '展开图片下载地址
  234.             print("------注:图片地址序号与下载线程编号,图片序号,异常序号一一对应,方便参考")
  235.             if a=="1":
  236.                 n=0
  237.                 for i in d:
  238.                     n+=1
  239.                     print("(",n,")  ",i)
  240.             # 打开崩溃日志
  241.             elif a=="2":
  242.                 n=0
  243.                 print("-----------------------------------------下载任务崩溃日志-------------------------------------------")
  244.                 if yichang==[]:
  245.                     print("------没有发生过崩溃异常哦~~")
  246.                 else:
  247.                     print("------注:异常序号与下载线程编号,图片序号,图片地址序号一一对应,方便参考")
  248.                     print("------以下是下载程序捕获的所有异常及参数:")
  249.                     for i in yichang:
  250.                         print("(",yichang_num[n],")  ",i)
  251.             # 退出程序
  252.             else:
  253.                 exit()
  254. # 捕捉终止快捷键
  255. except KeyboardInterrupt as reason:
  256.     k=time.strftime("%y-%m-%d  %H:%M:%S")
  257.     print("----------------------------------------------------------------------------------------------------------")
  258.     print("------ O__O …程序已无法运行!",k,reason)
  259.     print("------您按下了CTRL+C键,程序被强制终止!")
  260.     print("------如果您有任何使用问题,请报告:15838189794(微信QQ同号)")
  261.     print("------如想继续运行,请退出后重新打开此程序")
  262.     print("------ヾ ̄▽ ̄Bye~Bye~")
  263.     input()
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-10-31 09:28:02 | 显示全部楼层
好长~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-11-4 16:39:47 | 显示全部楼层
好多,厉害
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-7 03:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表