欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

如何利用python读取csv中url并下载?

程序员文章站 2022-05-01 18:37:49
如何利用python读取csv中url并下载? import urllib2 import pandas as pd import os 导入pandas库读取csv文...

如何利用python读取csv中url并下载?

import urllib2
import pandas as pd
import os

导入pandas库读取csv文件

data = pd.read_csv('train_attrs(2).csv')
data.head()
  id img_url xml_url
0 2 https://img1.ytgrading.com/sample/attr/2017/12/… https://img1.ytgrading.com/sample/attr/2017/12/…
1 3 https://img1.ytgrading.com/sample/attr/2017/12/… https://img1.ytgrading.com/sample/attr/2017/12/…
2 4 https://img1.ytgrading.com/sample/attr/2017/12/… https://img1.ytgrading.com/sample/attr/2017/12/…
3 5 https://img1.ytgrading.com/sample/attr/2017/12/… https://img1.ytgrading.com/sample/attr/2017/12/…
4 6 https://img1.ytgrading.com/sample/attr/2017/12/… https://img1.ytgrading.com/sample/attr/2017/12/…
data.iloc[1]
id                                                         3
img_url    https://img1.ytgrading.com/sample/attr/2017/12/...
xml_url    https://img1.ytgrading.com/sample/attr/2017/12/...
Name: 1, dtype: object

导入urllib2.urlopen下载jpg文件以及xml文件

for i in range(data.shape[0]):
    id1, pic, xml = data.iloc[i]
    f = urllib2.urlopen(pic)
    da = f.read()
    with open('./pic/{0}.jpg'.format(id1), 'wb') as code:
        code.write(da)
    f = urllib2.urlopen(xml)
    da = f.read()
    with open('./xml/{0}.xml'.format(id1), 'wb') as code:
        code.write(da)
    if i % 100 == 0:
        print i
0
100
200
300