看著自己寫的博客的訪問量低的可憐,有木有突然刷訪問量想法。然后小編便從網(wǎng)上找來了一份代碼試試刷下訪問量。但是刷完我就有深深的愧疚感,我們不應(yīng)該通過旁門歪道來獲得成功。這篇代碼,大家就來試試就好,千萬別拿來做壞事哦!
首先import urllib2是必須的。對于codecs,我本來以為要轉(zhuǎn)換輸出的格式,后來才發(fā)現(xiàn)并不需要。re是用來正則表達(dá)式匹配的。為了看起來比較舒服,我定義了一個(gè)CSDN的類。__init__(self)是用來賦初值的,由于我比較懶,直接把每篇博文的地址復(fù)制粘貼進(jìn)去(這樣每篇博文的訪問量同時(shí)增加,不會出現(xiàn)一篇博文10000+,其他都是10+的悲慘情況,不過我在這里聲明,我的博客在沒刷之前的訪問量也有6000+了,只不過翻倍了而已。激勵我更好的學(xué)習(xí)?。?,不過大家也可以通過進(jìn)入csdn_url并用正則表達(dá)式自動獲取每篇博文的地址。此外,我們一定要偽裝一個(gè)報(bào)頭,要不然網(wǎng)站不讓你進(jìn)入。所以剛開始我想用urllib.urlopen(csdn_url).read(),居然發(fā)現(xiàn)得到的文本是禁止訪問!而且為了直觀看我們的博客訪問數(shù)量的急劇變化,我設(shè)置了一個(gè)openCsdn函數(shù),并用正則表達(dá)式查到訪問數(shù)量。話不多說,代碼為證!
#-*- coding=utf-8 -*- import urllib2 import codecs import re csdn_url = "http://blog.csdn.net/walegahaha" blog_url = ["http://blog.csdn.net/walegahaha/article/details/51945421", "http://blog.csdn.net/walegahaha/article/details/51867904", "http://blog.csdn.net/walegahaha/article/details/51603040", "http://blog.csdn.net/walegahaha/article/details/50938260", "http://blog.csdn.net/walegahaha/article/details/50884627", "http://blog.csdn.net/walegahaha/article/details/50877906", "http://blog.csdn.net/walegahaha/article/details/50868049", "http://blog.csdn.net/walegahaha/article/details/50533424", "http://blog.csdn.net/walegahaha/article/details/50504522", "http://blog.csdn.net/walegahaha/article/details/50489053", "http://blog.csdn.net/walegahaha/article/details/50471417", "http://blog.csdn.net/walegahaha/article/details/50464531", "http://blog.csdn.net/walegahaha/article/details/50452959", "http://blog.csdn.net/walegahaha/article/details/50435986", ] class CSDN(object): def __init__(self): self.csdn_url = csdn_url self.blog_url = blog_url self.headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',} def openCsdn(self): req = urllib2.Request(self.csdn_url, headers = self.headers) response = urllib2.urlopen(req) thePage = response.read() response.close() pattern = "訪問:<span>(d+)次</span>" number = ''.join(re.findall(pattern, thePage)) print number def openBlog(self): for i in range(len(self.blog_url)): req = urllib2.Request(self.blog_url[i], headers = self.headers) response = urllib2.urlopen(req) response.close() for i in range(500): print i csdn = CSDN() csdn.openCsdn() csdn.openBlog() csdn.openCsdn()
【推薦課程:Python視頻教程】
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com