这是我需要下载的link。
我的问题是约会。我的示例代码不接受这种URL。你能帮帮我这个家伙吗?谢谢!
import urllib2
url = "wesm.ph/chart/export/luzon_dmd_csv_export.php?date=201705&hour=24"
file_name = url.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, file_size)
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
status = status + chr(8)*(len(status)+1)
print status,
f.close()
答案 0 :(得分:0)
您需要在上面的代码示例中进行两处更改。 将“http://”添加到网址。 应该按以下方式添加查询字符串:
url = "http://wesm.ph/chart/export/luzon_dmd_csv_export.php"
queryargs = { 'date':'201705', 'hour':'24' }
data = urllib.urlencode(queryargs)
u = urllib.urlopen(url,data)
代码最终应该如下:
import urllib2
import urllib
url = "http://wesm.ph/chart/export/luzon_dmd_csv_export.php"
queryargs = { 'date':'201705', 'hour':'24' }
data = urllib.urlencode(queryargs)
file_name = url.split('/')[-1].split('.')[-1]+'.csv'
u = urllib.urlopen(url,data)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, file_size)
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
status = status + chr(8)*(len(status)+1)
print status,
f.close()
答案 1 :(得分:0)
import requests
r = requests.get('http://wesm.ph/chart/export/luzon_dmd_csv_export.php?date=20160115&hour=24', stream=True)
with open('filename.csv', 'wb') as fd:
for chunk in r.iter_content(chunk_size=128):
fd.write(chunk)
只需使用requests