python爬取图片
import urllib.request import requests import json from openpyxl import workbook # 写入Excel表所用 from openpyxl import load_workbook # 读取Excel表所用 def get_img(headers, url): request = urllib.request.Request(url, headers=headers) response = urllib.request.urlopen(request) data = response.read() return data if __name__ == '__main__': wb = workbook.Workbook() ws = wb.active headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36'} ws.append( ["briefName", "photoName", "mobileVideoPath", "photoPath", "price", "priceAccurate", "productId", "promoLabels", "promotionInfo", "skuName", "webVideoPath"]) for n in range(1, 4): print(n) url = 'https://openapi.vmall.com/mcp/queryPrd?lang=zh-CN&country=CN&portal=1&keyword=36&pageSize=20&pageNum={}&searchSortField=0&searchSortType=desc&searchFlag=1&brandType=0&tid=c7a607e4827f084da988ab3816f08949&screenParams=%7B%7D&searchId=c7a607e4827f084da988ab3816f08949'.format(n) data = requests.get(url, headers=headers).text jsonData = json.loads(data) # with open('hw.txt','w') as file: # for d in jsonData['resultList']: # txt='"briefName":{},"photoName":{},"mobileVideoPath":{},"photoPath":{},' \ # '"price":{},"priceAccurate":{},"productId":{},"promoLabels":{},"promotionInfo":{}'\ # '"skuName":{},"webVideoPath":{}'\ # .format(d["briefName"],d["photoName"],d["mobileVideoPath"],d["photoPath"],d["price"], # d["priceAccurate"],d["productId"],d["promoLabels"],d["promotionInfo"],d["skuName"], # d["webVideoPath"]) # file.write(txt+'\n') for d in jsonData['resultList']: with open('E:\PyCharm项目\python\img\phone\\428_428_{}'.format(d["photoName"]), 'wb') as file: print('https://res.vmallres.com/pimages/{}428_428_{}'.format(d["photoPath"], d["photoName"])) # file.write('https://res.vmallres.com/pimages/{}428_428_{}'.format(d["photoPath"],d["photoName"])) file.write(get_img(headers, 'https://res.vmallres.com/pimages/{}428_428_{}'.format(d["photoPath"], d["photoName"]))) # print(type(d["promoLabels"])) ws.append( [d["briefName"], d["photoName"], d["mobileVideoPath"], d["photoPath"], d["price"], d["priceAccurate"], d["productId"], str(d["promoLabels"]), d["promotionInfo"], d["skuName"], d["webVideoPath"]]) wb.save('hw.xlsx')