# -*- coding: utf-8 -*-
'''美图录‘’‘
#-------------------------------------------------
import re
import os,stat
import time
import datetime
import urllib
import urllib.request
from bs4 import BeautifulSoup
import requests
from PIL import Image
from io import BytesIO
from threading import Thread
from multiprocessing import Process#-------------------------------------------------
class Web_IMG_Crawler(object):#初始化#def __init__(self,url,de_code):#self.url=url#self.de_code=de_code#self.soup=soup#self.file_path=file_path#self.file_name=file_name#self.count=count#反——反——爬——虫    def ant_Crawler(self,url):    header= {'User-Agent':'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)','Referer':''}FirstPAGE=re.findall(r"//(.+?)/",url)[0]Referer=str('http://'+FirstPAGE)header['Referer']=Refererreturn header#解析网址def JIEXI_WEB(self,url,de_code):open_url=urllib.request.urlopen(url)#请求网址并打开网址html=open_url.read().decode(de_code)#确定以什么编码读取网址///在浏览器中打开目标网址,右键即可查看目标网址的编码方式soup=BeautifulSoup(html,features="lxml")#创建 beautifulsoup 对象,并指定解析器为lxmlreturn soup    #网址list去重def QU_CHONG(self,all_url_list):end_list=[]  for element in all_url_list :if(element not in end_list):end_list.append(element)return end_list#-------------------------------------------------#保存图片def SAVE_IMG(self,file_path,url_img,file_name):try:#是否有这个路径if not os.path.exists(file_path):#创建路径os.makedirs(file_path)#获得图片后缀file_suffix = os.path.splitext(url_img)[1]print(file_suffix)#拼接图片名(包含路径)file_name ='{}{}{}{}'.format(file_path,os.sep,file_name,file_suffix)print(file_name)#下载图片,并保存到文件夹中urllib.request.urlretrieve(url_img,filename=file_name)except IOError as e:print("IOError")except Exception as e:print("Exception")def SAVE_IMAGE(self,IMG_link_set,url,file_path,file_name,sid):            """ def download(num):PAGE=str(sid+1)+'页'+str(num)+'张'file_name0=file_name+PAGE+'.jpg'html_IMG=requests.get(IMG_link_set[num],headers=Web_IMG_Crawler().ant_Crawler(url))try:image=Image.open(BytesIO(html_IMG.content))image.save(file_path+file_name0)print('第%s图片下载完成,图片名称:%s' % (PAGE,file_name0))except:print("IOError")  class My_imgThread(Thread):def __init__(self,num):Thread.__init__(self)self.num=numdef run(self):download(self.num)passthreadsimg=[]for num in range(len(IMG_link_set)):        j=My_imgThread(i)threadsimg.append(j)j.start()        for j in threads: j.join() """i=-1sum=1for url_img in IMG_link_set:i=i+1    url_img=IMG_link_set[i]PAGE=str(sid)+'页'+str(sum)+'张'file_name0=file_name+PAGE+'.jpg'sum=sum+1html_IMG=requests.get(url_img,headers=Web_IMG_Crawler().ant_Crawler(url))try:image=Image.open(BytesIO(html_IMG.content))image.save(file_path+file_name0)print('第%s图片下载完成,图片名称:%s' % (PAGE,file_name0))except:print("IOError")   #翻页网址更新获取def Nextall_page(self,soup,url):url_list=[]url_list0=[]url_list0.append(url)stype_url=re.findall(r"item/(.+?).html",url)[0]for list_url in soup.find_all('a',href=re.compile(stype_url)):urlget=list_url.get('href')url_list.append(urlget)url_list=Web_IMG_Crawler().QU_CHONG(url_list)web_url_len=int(re.findall(r"_(.+?).html",url_list[-1])[0])+1      urlf=re.findall(r"(.+?).html",url)[0]for urllast in range(2,web_url_len):url_list_all=urlf+'_'+str(urllast)+'.html'url_list0.append(url_list_all)return url_list0#获取图片网址listdef HQ_IMG_SQL(self,soup,url,file_path,file_name,sid):"""根据源输入网址特征获取目标网址方法stype_url=re.findall(r"item/(.+?).html",url)[0]src=re.compile(stype_url)src.get("src")"""stype_url=re.findall(r"item/(.+?).html",url)[0]url_IMG_list=[]IMG_link_set=[]for src in soup.find_all(src=re.compile(stype_url)):#提取图片网址url_img=src.get("src")url_IMG_list.append(url_img)IMG_link_set=Web_IMG_Crawler().QU_CHONG(url_IMG_list)#return IMG_link_setWeb_IMG_Crawler().SAVE_IMAGE(IMG_link_set,url,file_path,file_name,sid)def HQ_IMG_SQL2(self,soup):"""根据网址源代码中jpg特征,获取目标网址方法originalsrc=re.compile("jpg")src.get("originalsrc")"""url_IMG_list2=[]IMG_link_set2=[]for src in soup.find_all(originalsrc=re.compile("jpg")):#提取图片网址url_img=src.get("originalsrc")url_IMG_list2.append(url_img)IMG_link_set2=Web_IMG_Crawler().QU_CHONG(url_IMG_list2)return IMG_link_set2#-------------------------------------------------  def NAME_IMG(self,soup):"""获取图片名称,根据title检索title.get("title")"""for title in soup.find_all(originalsrc=re.compile("jpg")):#提取图片网址name_img0=title.get("title") return name_img0def NAME_IMG2(self,soup):"""获取图片名称,根据alt检索alt.get("alt")"""for alt in soup.find_all(src=re.compile("jpg")):#提取图片网址name_img2=alt.get("alt")return name_img2  def searchurl():search_url=input("请输入目标网址:",)#search_url=r'此处网址被和谐了'de_code=r'utf-8'soup=Web_IMG_Crawler().JIEXI_WEB(search_url,de_code)searchurl_list=[]for list_url in soup.find_all('a',href=re.compile('item')):searchurlget=list_url.get('href')searchurl_list.append(searchurlget)searchurl_list=Web_IMG_Crawler().QU_CHONG(searchurl_list)return searchurl_listdef TOopen(url):def thread_Open(sid):soup=Web_IMG_Crawler().JIEXI_WEB(NEXTurl_list[sid],de_code)Web_IMG_Crawler().HQ_IMG_SQL(soup,url,file_path,file_name,sid)    class My_Thread(Thread):def __init__(self,sid):Thread.__init__(self)self.sid=sid        def run(self):thread_Open(self.sid)passurl=url#url=r'此处网址被和谐了'#de_code=input("请输入网页编码类型:",) de_code=r'utf-8'soup=Web_IMG_Crawler().JIEXI_WEB(url,de_code)file_path='F:/图片/Saved Pictures/'#file_path='../Download/'file_name=Web_IMG_Crawler().NAME_IMG2(soup)NEXTurl_list=Web_IMG_Crawler().Nextall_page(soup,url)threads=[]for i in range(len(NEXTurl_list)):        t=My_Thread(i)threads.append(t)t.start()        for t in threads: t.join()      class My_searchThread(Process):def __init__(self,url):Process.__init__(self)self.url=urldef run(self):TOopen(self.url)pass
class My_searchThread0(Thread):def __init__(self,url):Thread.__init__(self)self.url=urldef run(self):TOopen(self.url)pass
#-------------------------------------------------
if __name__ == "__main__":begintime=time.strftime("%Y-%m-%d %H:%M:%S")starttime = datetime.datetime.now()threadssearch=[]for url in searchurl():        y=My_searchThread0(url)threadssearch.append(y)y.start()        for y in threadssearch: y.join() endtime=time.strftime("%Y-%m-%d %H:%M:%S")finishtime=datetime.datetime.now()runtime=(finishtime-starttime).secondstimespan='%f' %runtime    print( 'Time Begin:'+begintime)print( 'Time End:'+endtime)print( 'Time Span:'+timespan+' s')exit()

python爬虫多线程多进程示例相关推荐

  1. python代理ip多进程_静听网+python爬虫+多线程+多进程+构建IP代理池

    目标网站:静听网 网站url:http://www.audio699.com/ 目标文件:所有在线听的音频文件 附:我有个喜好就是晚上睡觉听有声书,然而很多软件都是付费才能听,免费在线网站虽然能听,但 ...

  2. 静听网+python爬虫+多线程+多进程+构建IP代理池

    目标网站:静听网 网站url:http://www.audio699.com/ 目标文件:所有在线听的音频文件 附:我有个喜好就是晚上睡觉听有声书,然而很多软件都是付费才能听,免费在线网站虽然能听,但 ...

  3. 创新实训-python爬虫多线程|解决中文乱码问题|卡片向上浮动效果|图文切换

    创新实训-python爬虫多线程|乱码问题|前端样式重新修改 考完毛概,把上周的工作总结一下.爬虫在第一周的时候只爬了一个就业指导这一个模块,这一次又加了招聘服务模块,所以就用了两个线程.前端首页一开 ...

  4. python爬虫 多线程 爬取网站排行榜上的所有小说

    python爬虫,多线程批量爬取多部小说 欢迎阅读 环境 获取排行榜top上的所有小说地址 分析排行榜top100页面的html,获取所有小说的url 遍历小说列表url到生成txt 多线程启动爬取任 ...

  5. python爬虫多线程下载_Python爬虫之多线程下载豆瓣Top250电影图片

    爬虫项目介绍 本次爬虫项目将爬取豆瓣Top250电影的图片,其网址为:https://movie.douban.com/top250, 具体页面如下图所示: 本次爬虫项目将分别不使用多线程和使用多线程 ...

  6. python爬虫多线程是什么意思_python爬虫中多线程的使用详解

    queue介绍 queue是python的标准库,俗称队列.可以直接import引用,在python2.x中,模块名为Queue.python3直接queue即可 在python中,多个线程之间的数据 ...

  7. Python爬虫多线程提升数据下载的性能优化

    Pyhton爬虫多线程提升数据下载的性能优化 很幸运地上了两次Tony老师关于python爬虫的课(收获巨多),在这里我对第一次课做一下知识总结: 1.什么是爬虫? 自动从网络上进行数据采集的程序 一 ...

  8. python爬虫多线程是什么意思_python爬虫14 | 就这么说吧,如果你不懂多线程和线程池,那就去河边摸鱼!...

    你知道吗? 在我的心里 你是多么的重要 就像 恩 请允许我来一段 freestyle 你们准备好了妹油 你看 这个碗 它又大又圆 就像 这条面 它又长又宽 你们 在这里 看文章 觉得 很开心 就像 我 ...

  9. Python中多线程多进程与协程的区别

    进程:一个运行的程序(代码)就是一个进程,没有运行的代码叫程序,进程是系统资源分配的最小单位,进程拥有自己独立的内存空间,所以进程间数据不共享,开销大. 线程: 调度执行的最小单位,也叫执行路径,不能 ...

最新文章

  1. android studio 执行不了,请问,Android studio程序不报错,但是一运行就stop是什原因...
  2. Resource interpreted as Script but transferred with MIME type text/plain:
  3. Android 应用内微信 H5 支付
  4. gpl可以商用吗_一文看懂开源许可证,能不能商用再也不抓瞎
  5. 【模拟】生日蛋糕(jzoj 1613)
  6. python变量名称跟着循环_python在循环中存储每次迭代使用不同名称的输入变量
  7. android 编译 sdl,SDL编译 - Android本地视频播放器开发_Linux编程_Linux公社-Linux系统门户网站...
  8. 新书出炉:我的视频我做主:Premiere Pro CS5实战精粹(1CD)(彩印)
  9. JDK 1.8 - 1.7
  10. java 怎么把日期格式化时间_如何实现java日期格式化?
  11. 奥斯汀页眉怎么设置_wps怎么只删除本页的页眉_Word页眉的设置和删除,这些问题你有遇到过吗?...
  12. 透明png图片和素材免费下载网站
  13. go+beego+gorilla实现上传保存图片
  14. 计算机音乐制作专业的大学,音乐制作专业哪些大学
  15. Leetcode 120 Triangle 三角形最小路径和
  16. mocha + chai
  17. 验证邮箱格式html代码,jquery验证邮箱格式是否正确实例讲解
  18. 谷歌google bard vs chatgpt给我的最大感受,速度真快,注册简单,多种答案提供。。。
  19. Android基本概念介绍
  20. Java二维数组声明与初始化

热门文章

  1. 持久化储存之IndexDB常用方法封装
  2. 微信小程序入门三之Flex布局
  3. 嘉兴桐乡考证培训-23年教资认定注意事项你知道吗?
  4. linux Futex同步机制
  5. Vue路由跳转的几种方式及解释说明
  6. 【C语言】14、通过键盘输入项数,计算“1+3+5+...+N”的值并进行输出,输出式举例如下:输入“11”,输出“第11 项为 21,1+3+5+...+21=121”。
  7. protostuff java_Protostuff一键序列化工具、Protobuf JAVA实现
  8. 化石能源类有哪些最新发表的毕业论文呢?
  9. vue与java一起打成jar包,前端与后端一起打成jar包
  10. 32.计算两个日期相隔的天数