远程获取文件

# coding:utf-8
import requests
import time
import os

url = "http://file.finance.sina.com.cn/211.154.219.97:9494/MRGG/CNSESH_STOCK/2018/2018-3/2018-03-29/4166991.PDF"

file = requests.get(url)
print(">>>>>>>>>>>>>>>")
print("11111111")
print(file.headers,11111)
print(file.request,11111)
print(file.content.__dir__(),11111)

print(">>>>>>>>>>>>>>>")

from student_sys.settings import BASE_DIR
path = os.path.join(BASE_DIR,'file','pdf_file')
if not os.path.exists(path):
    os.makedirs(path)
    print(path)
file_name = url.split('/')[-1]
print(file_name)
file_name = file_name.rsplit('_')[0]
print(file_name)
path = path + os.sep + 'a.pdf'
with open(path,'wb') as f:
    f.write(file.content)

  

>>>>>>>>>>>>>>>
11111111
{'Server': 'nginx/1.6.0 r19121211-d51c491', 'Content-Type': 'application/pdf', 'Connection': 'keep-alive', 'X-RequestId': '03f3971e-1912-2301-0602-782bcb67ca16', 'X-Requester': 'GRPS000000ANONYMOUSE', 'Date': 'Sun, 22 Dec 2019 17:06:03 GMT', 'Last-Modified': 'Wed, 28 Mar 2018 13:38:15 GMT', 'X-Filesize': '1422889', 'ETag': '"def47e2dbc564d9e86acfb4a3598c38f"', 'x-amz-meta-crc32': '1F743801', 'Cache-Control': 'max-age=31536000', 'Access-Control-Allow-Headers': 'Origin, Content-Type, Accept, Range, Content-Length', 'Access-Control-Allow-Methods': 'GET, PUT, POST, DELETE, OPTIONS, HEAD', 'Access-Control-Max-Age': '31536000', 'Access-Control-Allow-Origin': '*', 'Content-Length': '1422889'} 11111
<PreparedRequest [GET]> 11111
['__repr__', '__hash__', '__str__', '__getattribute__', '__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__', '__iter__', '__mod__', '__rmod__', '__len__', '__getitem__', '__add__', '__mul__', '__rmul__', '__contains__', '__new__', '__getnewargs__', 'capitalize', 'center', 'count', 'decode', 'endswith', 'expandtabs', 'find', 'fromhex', 'hex', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill', '__doc__', '__setattr__', '__delattr__', '__init__', '__reduce_ex__', '__reduce__', '__subclasshook__', '__init_subclass__', '__format__', '__sizeof__', '__dir__', '__class__'] 11111
>>>>>>>>>>>>>>>
4166991.PDF
4166991.PDF

原文地址:https://www.cnblogs.com/realadmin/p/12081849.html