python爬取emotioNet中的数据(通过爬取txt中的下载地址),如从dataFile_1010.txt开始:
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 19 14:57:31 2021
爬取emotion txt中的图片/
@author: S
"""
import os
import urllib.request
if __name__ == '__main__':
i=10
while i<96:
file=open(r'F:\emotionet\emotioNet_challenge_files_server_challenge_1.2_aws\dataFile_10'+str(i)+'.txt',"r")
i=i+1
line = file.readline()[:1]
for line in file.readlines():
# 下载要的图片
lines = line[0:87]
lines2 = line[53:64]
lines3 = line[53:87]
request = urllib.request.Request(lines)
try:
response = urllib.request.urlopen(request)
if response.getcode() == 200:
path2=r"F:\emotionet/Images/"+lines2
path3=r"F:\emotionet/Images/"+lines3
print(path2)
if not os.path.exists(path2):
print("mkdir")
os.makedirs(path2)
open(path3, 'wb').write(response.read()) # 将内容写入图片
print("done")
except:
open("F:\emotionet\error.txt","a").write(lines+"\n") #只需要将之前的”w"改为“a"即可,代表追加内容