[英]Downloading Many Files using Python Web-Scraping
如果我有指向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
如何编写网络抓取工具,以根据符号列表下载多个文件: [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的内容写入文件,还是将其自动下载到目录中?
您可以使用以下方法下载文件:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.