python爬取emotioNet

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"即可,代表追加内容
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值