# encoding: utf-8
import requests
from pymouse import PyMouse
from pykeyboard import *
import time
import pyperclip
import json
import os
import shutil
# 动态添加jQuery引用:
'''
var script = document.createElement('script');script.src = "https://cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.min.js";
document.head.appendChild(script);
'''
# 考拉海购-主图下载
'''
var arr = [];
$("#litimgUl img").each(function (index) {
var name = index+'';
var url = $(this).attr("src");
url = url.replace("?x-oss-process=image/resize,w_64/quality,q_85", "");
arr.push({
name: name,
url: url
});
});
var str1 = JSON.stringify(arr);
console.log(str1);
'''
# 淘宝分类名称和图片
'''
var arr = [];
$("ul.J_TSaleProp li a").each(function (index) {
var item = $("ul.J_TSaleProp li a")[index];
var url = $(item).attr("style");
if (url != null){
url = "http://" + url.replace("background:url(//", "").replace("_30x30.jpg) center no-repeat;", "");
}
var name = $(item).find("span").eq(0).text();
arr.push({
name: name,
url: url
});
});
var str1 = JSON.stringify(arr);
console.log(str1);
'''
# 淘宝详情图下载
'''
var arr = [];
var index=0;
$("#J_DivItemDesc p img").each(function (index) {
var url = $(this).attr("src");
index++;
arr.push({
name: index+'',
url: url
});
});
var str1 = JSON.stringify(arr);
console.log(str1);
'''
arr=[]
with open('data.json.txt', 'r') as f:# 从本地文件读取json数据
arr = json.load(f)
# 创建目录,存在就清空
# 原文:https://www.cnblogs.com/kaerxifa/p/13432173.html
def setDir(filepath):
if not os.path.exists(filepath):
os.mkdir(filepath)
else:
shutil.rmtree(filepath)
os.mkdir(filepath)
# 下载分类图片
def download():
setDir('./图片/')
print('准备下载图片!')
for inx, val in enumerate(arr):
print(inx)
name = str(inx+1) + '_' + val['name'] + '.jpg'
html = requests.get(val['url'])
with open("./图片/" + name , "wb") as f:
f.write(html.content)
# 复制内容道淘宝分类
def copy_taobao():
print('准备粘贴内容!')
m = PyMouse()
k = PyKeyboard()
time.sleep(5)
for inx, val in enumerate(arr):
print(inx)
pyperclip.copy(val['name'])
time.sleep(3)
k.press_key(k.control_key)
time.sleep(1)
k.tap_key('v')
time.sleep(1)
k.release_key(k.control_key)
time.sleep(1)
download()