python 抓取图片

来源:互联网 发布:铜陵安广网络 编辑:程序博客网 时间:2024/06/06 09:47
import urllib.request
import os
import json

wendangUrl = "https://wenku.baidu.com/browse/getbcsurl?doc_id=5b601ea9b0717fd5360cdca3&pn=1&rn=99999&type=ppt&callback=jQuery11010864977217700174_1503821431773&_=1503821431774";
wendangPath = "python培训课件";

os.mkdir(wendangPath);

def xxx(index ,mp3UrlString):
print("开始保存第" +str(index) + "张图片....");
response = urllib.request.urlopen(mp3UrlString);

response = response.read();

mp3F = open(wendangPath + "/" + str(index) + ".jpg" , "wb");

mp3F.write(response);

mp3F.close();
print("第" +str(index) + "张图片保存成功....");



headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'}
req = urllib.request.Request(url=wendangUrl,headers=headers)
urlResponse = urllib.request.urlopen(req).read().decode("utf8");

aaa = urlResponse.split("(");
bbb = aaa[1];
bbb = bbb[:-1];

zidian = json.loads(bbb);

for index , item in enumerate(zidian["list"]):
imageUrl = item["zoom"];
xxx(index , imageUrl);