如果我在Yahoo Finance上有CSV链接:http://ichart.finance.yahoo.com/table.csv?s=LOW&d=4&e=29&f=2014&g=d&a=8&b=22&c=1981&ignore=.csv
如何根据符号列表编写Web scraper来下载多个文件:[LOW, SPY, AAPL]
from StringIO import StringIO
from urllib2 import urlopen
for symbol in symbols:
f = urlopen ('http://www.myurl.com'+symbol+'therestoftheurl')
p = f.read()
d = StringIO(p)
f.close
我是否需要将url的内容写入文件,还是会自动下载到目录中?
答案 0 :(得分:0)
您可以使用此类方法下载文件:
import urllib2
file_name = "myfile.xyz"
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
block_sz = 4096
while True:
buffer = u.read(block_sz)
if not buffer:
break
f.write(buffer)
f.close()