python中Urllib模組應用執行個體

來源:互聯網
上載者:User

標籤:urllib模組

# -*-coding:utf-8-*-# urllib用於訪問不需要驗證的網路資源# urllib.urlretrave(url,tempfile, functionLodingProcess,[form_data])提供了下載資源的功能。所需參數如其名,# 不給定臨時檔案時,自動產生,返迴文件名,和執行資訊。# get 和post 是http的兩種常用的方法,get-->就象明信片方式[內容顯式放在URL中、內容長度受限、不安全]# post-->信封模式[內容放在信封裡,內容長度不受某些限制,安全]# 執行個體get方式:import urllibimport osdef reporthook(blocks_read, block_size, total_size):    """total_size is reported in bytes;    block_size is the amount read each time;    block_read is the number of  blocks successful read.    """    if not blocks_read:        print ‘connection opened‘        return    if total_size < 0:  # 沒有給定總大小就輸出已獲得多少資料        print ‘read %d blocks,(%d bytes)‘ % (blocks_read, blocks_read * block_size)    else:  # 給定了total_size值輸出獲得比值        amount_read = block_size * blocks_read        print ‘read %d blocks, or %d/%d‘ % (blocks_read, amount_read, total_size)    returntry:    filename, msg = urllib.urlretrieve(‘http://cve.scap.org.cn/CVE-2015-4785.html‘, reporthook=reporthook)finally:    print ‘File Exist?:‘, os.path.exists(filename), filename    txt = file(filename)    content = txt.read()    print content    urllib.urlcleanup()  # 清理臨時檔案    print ‘File Exist?:‘, os.path.exists(filename)


本文出自 “magicpwn” 部落格,請務必保留此出處http://magicpwn.blog.51cto.com/10497784/1679034

python中Urllib模組應用執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.