下载静态网页源代码的 python 爬虫函数源代码:
import urllib2
def download(url, num_retries = 5):
'''
function: 下载网页源代码,如果遇到 5xx 错误状态,则继续尝试下载,直到下载 num_retries 次为止。
'''
print "downloading " , url
try:
html = urllib2.urlopen(url).read()
except urllib2.URLError as e:
print "download error: " , e.reason
html = None