简体   繁体   English

使用Python网络抓取下载许多文件

[英]Downloading Many Files using Python Web-Scraping

If I have a link to a CSV on Yahoo Finance: 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 如果我有指向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

how would I write a web scraper to download multiple files based on a list of symbols: [LOW, SPY, AAPL] 如何编写网络抓取工具,以根据符号列表下载多个文件: [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

Do I need to write the contents of the url to file, or will it download automatically into a directory? 我需要将url的内容写入文件,还是将其自动下载到目录中?

You can use a method like this to download files: 您可以使用以下方法下载文件:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM