使用python爬取csdn博客访问量

# -*- coding: utf-8 -*-


import urllib2
import re

#当前的博客列表页号
page_num = 1
#不是最后列表的一页,如果有尾页说明不是最后一页
notLast = 1

account = "mrzhang628"

#首页地址
baseUrl = 'http://blog.csdn.net/'+account

while notLast:
	#连接页号,组成爬取的页面网址
	myUrl = baseUrl+'/article/list/' + str(page_num)

	#伪装成浏览器访问,直接访问的话csdn会拒绝
	user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; windows NT)'
	headers = {'User-Agent':user_agent}
	#构造请求
	req = urllib2.Request(baseUrl,headers=headers)

	#request = urllib2.Request(myUrl);
	#request.add_header(headers);	

	#print("req = ",str(req));

	#访问页面
	try:
		myResponse = urllib2.urlopen(req) #timeout用来设置超时
	except Exception, e:
		raise
	else:
		pass #Python pass是空语句,是为了保持程序结构的完整性。
	finally:
		pass


	myPage = myResponse.read()

	#print("notLast = " ,str(notLast));

	print '-----------------------------第%d页---------------------------------' % (page_num,)

	#利用正则表达式来获取博客的标题
	titles = re.findall('<span class="link_title"><a href=".*?">(.*?)</a></span>',myPage,re.S)	
	titleList=[] #表用[ ]标识。是python最通用的复合数据类型
	for items in titles:
		titleList.append(str(items).lstrip().rstrip())#list.append(obj):在列表末尾添加新的对象

	#利用正则表达式获取博客的访问量
	views = re.findall('<span class="link_view".*?><a href=".*?" title="阅读次数">阅读</a>\((.*?)\)</span>',myPage,re.S)
	viewList=[]
	for items in views:
		viewList.append(str(items).lstrip().rstrip()) 


	#这里相当于创造一个迭代器
	index = []
	count = 0
	while (count < len(viewList)):
		index.insert(count,count)
		count = count + 1
	
	print(index)
	
	#将结果输出 
	for n in index: #range(len(titleList)):
		print '访问量:%s 标题:%s' % (viewList[n].zfill(4),titleList[n])

	#页号加1
	page_num = page_num + 1

	#在页面中查找是否存在‘尾页’这一个标签来判断是否为最后一页
	notLast = re.findall('<a href=".*?">尾页</a>',myPage,re.S)


有问题请及时交流学习,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值