使用Python urllib2下载CSDN博客列表到本地

# -*- coding: utf-8 -*-
import string, urllib2

# 下载CSDN博客列表
def load_csdn(url, page):
    name = string.zfill(page, 5) + '.html'
    print '正在下载' + str(page) + '个页面,并存储其为' + name

    f = open(name, 'w+')
    url = url + '/' + str(page)
    print url

    # 伪装成浏览器
    headers = {
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36'
    }
    req = urllib2.Request(url = url,headers = headers)
    m = urllib2.urlopen(req).read()
    
    f.write(m)
    f.close()

# http://blog.csdn.net/Geek_ymv/article/list/2

url = str(raw_input(u'请输入地址:'))
page = int(raw_input(u'请输入第几页:'))

# 下载网页到本地
load_csdn(url, page)
    

你可能感兴趣的:(使用Python urllib2下载CSDN博客列表到本地)