新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:用python可以下載網(wǎng)頁源碼嗎
python可以下載網(wǎng)頁的源代碼,使用urllib庫,或者使用更為方便的requests庫。

import urllib2 def download(url, num_retries = 5): ''' function: 下載網(wǎng)頁源代碼,如果遇到 5xx 錯誤狀態(tài),則繼續(xù)嘗試下載,直到下載 num_retries 次為止。 ''' print "downloading " , url try: html = urllib2.urlopen(url).read() except urllib2.URLError as e: print "download error: " , e.reason html = None if num_retries > 0: if hasattr(e,'code') and 500 <= e.code < 600: return download(url, num_retries-1) return html
其中 url 即為你想現(xiàn)在的網(wǎng)頁地址。 num_reties 為遇到 5xx 錯誤的時候,重試下載的次數(shù)。
更多學習內容,請點擊python學習網(wǎng)。
名稱欄目:創(chuàng)新互聯(lián)Python教程:用python可以下載網(wǎng)頁源碼嗎
分享路徑:http://m.fisionsoft.com.cn/article/codjeji.html


咨詢
建站咨詢
