[source code] Python Programming Tutorial - 24 - Downloading Files from the Web

+1 Bucky Roberts · September 2, 2014

from urllib import request

goog_url = 'http://real-chart.finance.yahoo.com/table.csv?s=GOOG&d=8&e=2&f=2014&g=d&a=2&b=27&c=2014&ignore=.csv'


def download_stock_data(csv_url):
    response = request.urlopen(csv_url)
    csv = response.read()
    csv_str = str(csv)
    lines = csv_str.split("\\n")
    dest_url = r'goog.csv'
    fx = open(dest_url, "w")
    for line in lines:
        fx.write(line + "\n")
    fx.close()

download_stock_data(goog_url)

Post a Reply

Replies

Oldest  Newest  Rating
0 Arthur lee · December 9, 2014
hi, i have some questions. 
1.what does "request.urlopen(csv_url)"return to response?(address or others)
2what does the "csv"get from "response.read()",why we should use "str()"?
thanks!!
  • 1

Python

107,072 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator