【问题标题】:Downloading Many Files using Python Web-Scraping使用 Python Web-Scraping 下载许多文件
【发布时间】:2014-05-29 15:53:56
【问题描述】:

如果我有 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的内容写入文件,还是会自动下载到目录中?

【问题讨论】:

    标签: python csv web-scraping


    【解决方案1】:

    您可以使用这样的方法下载文件:

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-27
      • 2017-10-13
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 2017-10-31
      • 2018-08-24
      相关资源
      最近更新 更多