python下载文件保存_Python通过存储在csv中的链接下载文件

1586010002-jmsa.png

As a newbie in Python (2.7) I`m looking for next suggestion:

I have a csv file with stored http links in one column comma delimited.

http://example.com/file.pdf,

http://example.com/file.xls,

http://example.com/file.xlsx,

http://example.com/file.doc,

The main aim is to loop through all these links and download files by them in original extention and name.

So my search results and help here gave me next script:

import urllib2

import pandas as pd

links = pd.read_csv('links.csv', sep=',', header =(0))

url = links # I know this part wrong by don`n know how to do right

user_agent = 'Mozilla 5.0 (Windows 7; Win64; x64)'

file_name = "tessst" # here the files name by how to get their original names

u = urllib2.Request(url, headers = {'User-Agent' : user_agent})

req = urllib2.urlopen(u)

f = open(file_name, 'wb')

f.write(req.read())

f.close()

please any help

P S not sure about pandas - maybe csv better?

解决方案

If I can assume your CSV file to be one column only, containing links then this would work .

import csv, sys

import requests

import urllib2

import os

filename = 'test.csv'

with open(filename, 'rb') as f:

reader = csv.reader(f)

try:

for row in reader:

if 'http' in row[0]:

#print row

rev = row[0][::-1]

i = rev.index('/')

tmp = rev[0:i]

#print tmp[::-1]

rq = urllib2.Request(row[0])

res = urllib2.urlopen(rq)

if not os.path.exists("./"+tmp[::-1]):

pdf = open("./" + tmp[::-1], 'wb')

pdf.write(res.read())

pdf.close()

else:

print "file: ", tmp[::-1], "already exist"

except csv.Error as e:

sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值