import requests
import threading
from queue import Queue
from urllib import parse
import os
from urllib import request
url_name=[]#文件夹名列表
url_link=[]#全图链列表
class Get_url():#获取url和html的内容
def __init__(self,url,headers):
self.url=url
self.headers=headers
def run(self):
resp=requests.get(self.url,headers=self.headers)
return resp.json()
# 生产者
class Get_link(threading.Thread):#获取图链和图链名称,文件夹名称
def __init__(self,all_url,image_url_queue):
super().__init__()
self.all_url=all_url
self.image_url_queue=image_url_queue
def run(self):
global url_link
global url_name
headers = {
'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Mobile Safari/537.36'}
# 获取全链接连表
for pin_url in self.all_url:
true_url=pin_url['List']
for true_url1 in true_url:
url_link1=[]
for i in range(1,9):
true_url2=true_url1['sProdImgNo_{}'.format(i)]
result1 = parse.unquote(true_url2).replace('/200','/0')
url_link1.append(result1)
url_link.append(url_link1)
# print(url_link)
# 获取全名称列表
for name in self.all_url:
true_name = name['List']
url_name1 = []
for true_name1 in true_name:
# url_name=[]
true_name2 = true_name1['sProdName']
result2 = parse.unquote(true_name2)
url_name.append(result2)
# print(url_name)
d=dict(zip(url_name,url_link))
# 创建和判断目录存在
for key in d:
filename='../王者荣耀案例/王者荣耀高清壁纸/'+key
is_exists=os.path.exists(filename)
if not is_exists:
try:
os.makedirs(filename)
except NotADirectoryError:
print(key+'目录创建成功')
print(key+'目录创建成功')
else:
print(key+'目录已存在')
# 生产图链并存入队列
for index, image_url in enumerate(d[key]): # 生成图片名的数字序列
self.image_url_queue.put({'image_path':os.path.join(filename,f'{index+1}.jpg'),'image_url':image_url})
# 消费者
class Storage(threading.Thread):
def __init__(self,image_url_queue):
super().__init__()
self.image_url_queue=image_url_queue
def run(self):
# headers = {'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Mobile Safari/537.36'}
# m=1
# image_obj=self.image_url_queue.get(timeout=20)
# resp=requests.get(image_obj[1],headers=headers)
# with open(image_obj[0]+'/'+str(m)+'.jpg','wb') as f:
# f.write(resp.content)
# print(image_obj[0]+'下载完毕!')
# m+=1
while True:
try:
image_obj = self.image_url_queue.get(timeout=20)
request.urlretrieve(image_obj['image_url'], image_obj['image_path'])
print(f'{image_obj["image_path"]}下载完成')
except:
break
def start():
all_url = []
for i in range(1,3):
url = 'https://apps.game.qq.com/cgi-bin/ams/module/ishow/V1.0/query/workList_inc.cgi?activityId=2735&sVerifyCode=ABCD&sDataType=JSON&iListNum=20&totalpage=0&page={}&iOrder=0&iSortNumClose=1&jsoncallback=&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735&_=1637683982984'.format(i)
headers = {'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Mobile Safari/537.36'}
# 1获取地址
get_url=Get_url(url,headers)
pin_url=get_url.run()
all_url.append(pin_url)
#2建立队列类
image_url_queue=Queue(1000)
# 3创建生产者线程对象
for i in range(50):
t = Get_link(all_url,image_url_queue)
t.start()
# 4创建消费者线程对象
for i in range(100):
t = Storage(image_url_queue)
t.start()
if __name__ == '__main__':
start()
```python
在这里插入代码片
|