python 下载文件到文件夹下的问题
#-*-coding:utf-8-*-importurllib2re=urllib2.Request(r'http://www.google.cn/intl/zh-CN/...
#-*- coding:utf-8 -*-
import urllib2
re = urllib2.Request(r'http://www.google.cn/intl/zh-CN/images/logo_cn.gif')
rs = urllib2.urlopen(re).read()
open('google.gif', 'wb').write(rs)
这段代码是说把链接地址中现在下载下来的logo_cn.gif,以2进制的方式写到本地google.gif文件中吗?但我是用的爬虫循环好多下载地址,要怎么把它保存到本地磁盘的文件夹下(如:r'd:\download') 展开
import urllib2
re = urllib2.Request(r'http://www.google.cn/intl/zh-CN/images/logo_cn.gif')
rs = urllib2.urlopen(re).read()
open('google.gif', 'wb').write(rs)
这段代码是说把链接地址中现在下载下来的logo_cn.gif,以2进制的方式写到本地google.gif文件中吗?但我是用的爬虫循环好多下载地址,要怎么把它保存到本地磁盘的文件夹下(如:r'd:\download') 展开
3个回答
展开全部
方法一:
import urllib
import urllib2
import requests
print "downloading with urllib"
url = 'http://www.pythontab.com/test/demo.zip'
print "downloading with urllib"
urllib.urlretrieve(url, "demo.zip")
方法二:
import urllib2
print "downloading with urllib2"
url = 'http://www.pythontab.com/test/demo.zip'
f = urllib2.urlopen(url)
data = f.read()
with open("demo2.zip", "wb") as code:
code.write(data)
方法三:
import requests
print "downloading with requests"
url = 'http://www.pythontab.com/test/demo.zip'
r = requests.get(url)
with open("demo3.zip", "wb") as code:
code.write(r.content)
import urllib
import urllib2
import requests
print "downloading with urllib"
url = 'http://www.pythontab.com/test/demo.zip'
print "downloading with urllib"
urllib.urlretrieve(url, "demo.zip")
方法二:
import urllib2
print "downloading with urllib2"
url = 'http://www.pythontab.com/test/demo.zip'
f = urllib2.urlopen(url)
data = f.read()
with open("demo2.zip", "wb") as code:
code.write(data)
方法三:
import requests
print "downloading with requests"
url = 'http://www.pythontab.com/test/demo.zip'
r = requests.get(url)
with open("demo3.zip", "wb") as code:
code.write(r.content)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
直接os.system('dir C:\Perl ')
或者 os.chdir(dirname) 改变当前工作目录
或者 os.chdir(dirname) 改变当前工作目录
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询