python urllib包 實現通過http下載文件


為了更通用我們來用HTTPPasswordMgrWithDefaultRealm密碼管理器來實現

import urllib.request as urllib2

url = 'http://202.108.1.51'
user = 'a'
passwd = 'aa'
# 登陸連接url
psmg = urllib2.HTTPPasswordMgrWithDefaultRealm()
psmg.add_password(None,url,user,passwd)
hdlr = urllib2.HTTPBasicAuthHandler(psmg)
opener = urllib2.build_opener(hdlr)
urllib2.install_opener(opener)
# 打開url地址文件
f = urllib2.urlopen(url)
for i in f:
    print(i)
with open(os.path.join(os.path.dirname(os.path.abspath("__file__")), 'xxx.txt'), "w+") as f:
for i in r:
a = i.strip().decode() + '\n'
f.write(a)

 更多信息見:   參數詳解 https://blog.csdn.net/qq_41856814/article/details/99658108

      http://www.cnblogs.com/ubunoon/archive/2010/08/25/1807970.html

                       http://blog.csdn.net/adrianfeng/article/details/5864510


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM