import string,urllib.request
def cookie(get_page,begin,end):
for i in range(begin,end+1):
sname = 'f:/test/' + '网页' + str(i).zfill(3) + '.html'
print ('正在下载第' + str(i) + '个页面...')
m = urllib.request.urlopen(get_page+str(i)).read().decode('gbk')
with open(sname,'w+') as file:
file.write(m)
get_page = str(input('请输入贴吧的地址,去掉pn=后面的数字:\n'))
begin = int(input('请输入开始的页数:\n'))
end = int(input('请输入终点的页数:\n'))
#调用
cookie(get_page,begin,end)
一个问题,保存下来的网页要用
m = urllib.request.urlopen(get_page+str(i)).read().decode('gbk')
这样加上 .decode('gbk') 之后才能保存,否则打开文字全部都是乱码
抓取网页到本地,参考博客 网络爬虫(六):一个简单的百度贴吧的小爬虫