python2.7爬虫脚本实现刷取CSDN博客访问量。
所需要的库:
- re
- urllib2
- time
其中re库是为了爬虫爬取网页之后可以使用正则表达式匹配当前文章的阅读数,从而更加直观的查看当前访问量的变化。
由于CSDN博客的访问量在一分钟内只能增加一次。所以引入了time这个库,通过sleep()函数让爬虫歇息1分钟,然后开始下一次的刷取。
urllib2是一个爬虫库,我是通过这个库,访问代理api接口,从而拿到代理地址。
所要用到的正则表达式:
- 匹配文章访问量所在标签:
count_pattern = "<span class=\"read-count\">(.*?)</span>"
- 匹配文章标题内容所在标签:
pattern = "<h1 class=\"title-article\">(.*?)</h1>"
- 匹配文章访问量在标签中的位置:
pattern='<[^>]+>'
- 匹配文章标题在标签中的位置:
pattern="d.*?[\d]?"
关于代理
代理我是用的是蘑菇隧道代理,首次注册的用户可以赠送一个可免费使用一天的api接口,我们可以通过这个接口接入到蘑菇隧道的代理池,通过这个代理池拿到代理ip从而防止本机地址被CSDN封停或是加入黑名单。
当然你们也可以使用别的ip代理。
关于源代码
# coding=utf-8
import time
import urllib2
import re
#待刷取的文章列表
url = [
"https://blog.csdn.net/qq_27180763/article/details/82556058",
"https://blog.csdn.net/qq_27180763/article/details/82683029",
"https://blog.csdn.net/qq_27180763/article/details/82556316",
"https://blog.csdn.net/qq_27180763/article/details/82556565",
"https://blog.csdn.net/qq_27180763/article/details/82556807",
"https://blog.csdn.net/qq_27180763/article/details/82556807",
"https://blog.csdn.net/qq_27180763/article/details/82558420",
"https://blog.csdn.net/qq_27180763/article/details/82588136",
"https://blog.csdn.net/qq_27180763/article/details/82588194",
"https://blog.csdn.net/qq_27180763/article/details/82588305",
"https://blog.csdn.net/qq_27180763/article/details/82656677",
"https://blog.csdn.net/qq_27180763/article/details/82662489",
"https://blog.csdn.net/qq_27180763/article/details/82683827",
"https://blog.csdn.net/qq_27180763/article/details/82698878",
"https://blog.csdn.net/qq_27180763/article/details/82731987",
"https://blog.csdn.net/qq_27180763/article/details/82735100"
]
count_pattern = "<span class=\"read-count\">(.*?)</span>"
article_pattern = "<h1 class=\"title-article\">(.*?)</h1>"
#如果不使用代理api,则直接在这里写入代理ip,要求为字典类型。
'''
proxy = {'0': 'http://58.218.92.176:38095',
'1': 'http://121.8.98.196:80',
'2': 'http://101.227.5.36:9000',
}
'''
while(True):
//不使用代理api则采用下面这种方法
'''
for value in proxy.values():
value = value.split("//")
temp = dict()
temp["http"] = value[1]
# 创建ProxyHandler
proxy_handler = urllib2.ProxyHandler(temp)
# 创建Opener
opener = urllib2.build_opener(proxy_handler)
# 安装Opener
urllib2.install_opener(opener)
'''
#请替换appKey(下面那个是我的apikey,估计现在已经失效了。)
for i in range(0, len(url)):
appKey = "ZE52bFRZYU5yRUlhamNvTzpFNVk2VlB1cm1QT0lyU3Rx"
# 接下来使用蘑菇隧道代理进行访问
proxy_handler = urllib2.ProxyHandler({"http": 'transfer.mogumiao.com:9001'})
opener = urllib2.build_opener(proxy_handler)
request = urllib2.Request(url[i])
request.add_header('Proxy-Authorization', 'Basic ' + appKey)
response = opener.open(request)
#CSDN博客返回的HTML源码的编码格式为UTF-8
HTML = response.read().decode('UTF-8')
count = re.search(count_pattern, HTML)
article = re.search(article_pattern, HTML)
number = re.search(""\d.*?[\d]?"", count.group())
p = re.compile('<[^>]+>')
name = p.sub("", article.group())
print "当前文章 《"+ name.encode('utf-8') + "》\t阅读量"+str(number.group())
print "==============================================================\n"
time.sleep(60)
以上即为CSDN刷取访问量的源代码,仅供学习参考,不得用于商业用途。