#-*- coding:utf-8 -*-
import requests
print('start')
url = r'问号前面的网址'
parms = {
'fileId' : '123 '
}
try:
r = requests.post(url, data=parms)
print(r.text)
except Exception as e:
print(e)
知道文件的url地址就用urllib模块的urlretrieve函数。urllib.urlretrieve(url,filename)filename是要保存到本地的文件名。函数后面还有2个可选参数,要用就看帮助文档吧。多线下载的话,每一线程要指定下载服务器上文件的哪一块。http协议中head里可以指定Range。下面用的是urllib2模块request
=
urllib2.Request(url)request.add_header("Range",
"bytes=%d-%d"%(1024,
204)
#指定下载文件的范围opener
=
urllib2.build_opener()data
=
opener.open(request).read()现在data里面就是文件的1024字节到2048字节的内容。
可以使用requests模块完成下载
# --------------------第一种:使用headers携带cookie-----------------------------# coding = utf-8
import requests
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1 Win64 x64),
'Cookie':'你的cookies',
}
# 发起请求,获取二进制数据
html_str = requests.get(url,headers=headers).content
# 写入文件,采用二进制写入文件
with open('路径/文件名.后缀','wb') as f:
f.write(html_str)
# --------------------第二种:在requests直接携带--------------------------------
# coding = utf-8
import requests
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1 Win64 x64),
}
cookies = {你的cookies,这里必须是以键值对,也就是字典的形式}
# 发起请求,获取二进制数据
html_str = requests.get(url,headers=headers,cookies = cookies).content
# 写入文件,采用二进制写入文件
with open('路径/文件名.后缀','wb') as f:
f.write(html_str)