Question about Downloading files from the web

+1 Shlesh Tiwari · November 9, 2015
Hi!

Just a couple of questions. 

1. Why didn't we use the urlopen() in the "Downloading an image" tutorial? 
2. How does the line split know where to split?

Thanks!

Post a Reply

Replies

Oldest  Newest  Rating
0 alex constantin · November 9, 2015
Code please?
0 Shlesh Tiwari · November 9, 2015
Sorry about that. Here it is. 

import random
import urllib

def download_web_image(url):
name = random.randrange(1,100)
fullname = str(name)+ ".jpg"
urllib.urlretrieve(url,fullname)

image = urllib.urlopen(url)
response=image.read()
fw=open('Image.jpg','w')
fw.write(response)
fw.close()

download_web_image("http://i.telegraph.co.uk/multimedia/archive/01757/Alleyne_60_1757330j.jpg")

Another one not involving an image:


import urllib
y_url = 'http://real-chart.finance.yahoo.com/table.csv?s=SCO&d=10&e=9&f=2015&g=d&a=10&b=25&c=2008&ignore=.csv'

def file_download(url):
name=urllib.urlretrieve(url,'yahoo1.csv')

name = urllib.urlopen(url)
csv_read=name.read()
csv_str=str(csv_read)
csv = csv_str.split('\\n')
fw=open('yahoo.csv','w')
for i in csv:
fw.write(i)
fw.close()


file_download(y_url)



Thanks! 
  • 1

Python

107,161 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator