最近老婆大人的公司給老婆大人安排了一個根據關鍵詞查詢google網站排名的差事。老婆大人的公司是做seo的,查詢的關鍵詞及網站特別的多,看著老婆大人這么辛苦的重復著查詢工作,心疼啊。所以花點時間用python寫了一個根據關鍵詞搜索網站排名的py腳本。
在寫這個腳本之前,我也曾在網站搜索過關于在google查排名的腳本。很多是利用google的api。但是我測試了一下,不準。所以,自己寫一個吧。
腳本內容如下:(關鍵詞我在網站隨便找了幾個。以做測試使用)
#vim keyword.py import urllib,urllib2,cookielib,re,sys,os,time,random cj = cookielib.CookieJar() vibramkey=['cheap+five+fingers','vibram+five+fingers'] beatskey=['beats+by+dre','beats+by+dre+cheap'] vibramweb=['vibramforshoes.com','vibramfivetoeshoes.net','vibramfivefingersshoesx.com '] beatsweb=['beatsbydre.com','justlovebeats.com'] allweb=['vibramweb','beatsweb'] def serchkey(key,start): url="http://www.google.com/search?hl=en&q=%s&revid=33815775&sa=X&ei=X6CbT4GrIoOeiQfth43GAw&ved=0CIgBENUCKAY&start=%s" %(key,start) try: opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) opener.addheaders = [('User-agent', 'Opera/9.23')] urllib2.install_opener(opener) req=urllib2.Request(url) response =urllib2.urlopen(req) content = response.read() f=open('google','w') f.write(content) tiqu=os.popen("grep -ioP '(?<=<cite>).*?(?=</cite>)' google|sed -r 's/(<*//*cite>|<//*b>)//g'").readlines() except: changeip() else: for yuming in pinpai: a=1 for shouyuming in tiqu: real=shouyuming.find(yuming) if real>0: if start==0: page=1 elif start==10: page=2 elif start==20: page=3 elif start==30: page=4 else: page=5 lastkey=key.replace("+"," ") xinxi="%s/t/t %s/t/t page%s,%s<br>/n" %(yuming,lastkey,page,a) xinxifile=open('index.html','a') xinxifile.write(xinxi) xinxifile.close() a=a+1 def changeip(): ip=random.randint(0,2) de="route del -host google.com" add="route add -host google.com eth1:%s" %ip os.system(de) os.system(add) print "changip to %s" %ip pinpaiid=0 for x in vibramkey,beatskey: if pinpaiid == 0: pinpai=vibramweb elif pinpaiid == 1: pinpai=beatsweb pinpaiid=pinpaiid+1 for key in x: for start in 0,10,20,30,40: serchkey(key,start) changeip() os.system("sh paiban.sh")
#vim paiban.sh #! /bin/bash sort index.html -o index.html line=`wc -l index.html|awk '{print $1}'` yuming2=`sed -n 1p index.html|awk '{print $1}'` for i in `seq 2 $line` do yuming=`sed -n "$i"p index.html|awk '{print $1}'` if [ $yuming == $yuming2 ];then sed -i ""$i"s/"$yuming"//t/t/g" index.html else yuming2=$yuming fi done
這段腳本分兩部分,第一部分是python利用關鍵詞搜索google的頁面。老婆大人說只要每一個關鍵詞的前5頁就可以。所以只查詢了前5頁。
第二部分是將查詢出來的結果進行排版。也就是最下面調用paiban.sh 所做的事情,讓最終出來的結果為如下格式:
網站1 關鍵詞1 第幾頁 第幾名
關鍵詞2 第幾頁 第幾名
關鍵詞3 第幾頁 第幾名
網站2 關鍵詞1 第幾頁 第幾名
關鍵詞2 第幾頁 第幾名
關鍵詞3 第幾頁 第幾名
下面就來對程序進行講解。
import urllib,urllib2,cookielib,re,sys,os,time,random #加載模塊 cj = cookielib.CookieJar() vibramkey=['cheap+five+fingers','vibram+five+fingers'] #定義要查詢的關鍵詞組1,里面的單引號里面就是要查詢的關鍵詞。 beatskey=['beats+by+dre','beats+by+dre+cheap'] #同上,定義關鍵詞組2,這個是另一組關鍵詞。 vibramweb=['vibramforshoes.com','vibramfivetoeshoes.net','vibramfivefingersshoesx.com '] #定義關健詞組1要查詢的網站 beatsweb=[' beatsbydre.com',' justlovebeats.com'] #定義關健詞組2要查詢的網站 allweb=['vibramweb','beatsweb'] #這里定義了一個所有網站的組,下面好調用。 def serchkey(key,start): #這里定義一個函數,key為查詢的關健詞,start為頁面,通過google查詢頁面可以看出來每個頁面除ads外只有十條記錄,start=0時顯示為第一個頁面第一至第十條記錄,start=10時,顯示第二頁的第一至十條記錄,以些類推。 url="http://www.google.com/search?hl=en&q=%s&revid=33815775&sa=X&ei=X6CbT4GrIoOeiQfth43GAw&ved=0CIgBENUCKAY&start=%s" %(key,start) #這個定義了查詢的URL try: opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) opener.addheaders = [('User-agent', 'Opera/9.23')] #模擬瀏覽器訪問 urllib2.install_opener(opener) req=urllib2.Request(url) #用urllib2訪問 response =urllib2.urlopen(req) content = response.read()#這塊是模擬瀏覽器進行訪問url的頁面并讀取源代碼 f=open('google','w') f.write(content) #將讀取出來的內容保存到google的一個頁面里。 tiqu=os.popen("grep -ioP '(?<=<cite>).*?(?=</cite>)' google|sed -r 's/(<*//*cite>|<//*b>)//g'").readlines() #這里利用了系統命令了。利用正則的零寬斷言提直接取出第一到第十位的網站域名。 except: changeip() #這邊是怕訪問過多被google封了。所以這里有一個換ip的函數,下面有定義。上面如果try失敗了,就執行換ip的動作。 else: for yuming in pinpai: #循環讀取要查找的網站 a=1 for shouyuming in tiqu: #循環讀取查找出來的網站 real=shouyuming.find(yuming) #將查找出來的網站與需要查找的網站進行比對 if real>0: if start==0: page=1 elif start==10: page=2 elif start==20: page=3 elif start==30: page=4 else: page=5 #這里的查看域名在google搜索后的哪一頁。 lastkey=key.replace("+"," ") #將定義的關鍵詞中間的加號去掉。 print yuming,lastkey,page,a xinxi="%s/t/t %s/t/t 第%s頁,排名%s/n" %(yuming,lastkey,page,a) xinxifile=open('index.html','a') xinxifile.write(xinxi) xinxifile.close() #將查找出來的信息寫入到index.html文件里 aa=a+1 def changeip(): #這里是定義查詢時換ip的函數。如果機器只有一個ip那就不用這段了。 ip=random.randint(0,10) #隨機生成0-10的數 del="route del -host google.com" #刪除路由命令 add="route add -host google.com eth1:%s" %ip #添加路由命令 os.system(del) #執行刪除路由命令 os.system(add) #執行添加路由命令 print "changip to %s" %ip #打印更改路由信息 pinpaiid=0 for x in vibramkey,beatskey: #循環所有的關鍵詞組 if pinpaiid == 0: # 對應關鍵詞組與要查詢的網站組 pinpai=vibramweb elif pinpaiid == 1: pinpai=beatsweb pinpaiidpinpaiid=pinpaiid+1 for key in x: #循環關鍵詞組里的關鍵詞 for start in 0,10,20,30,40: #定義所要查找的google的頁面 serchkey(key,start) changeip() #更改ip函數。在每一組關鍵詞查詢完畢后更改ip.
以上命令執行后,我們看一下index.html文件內容。如下:
#cat index.html
vibramforshoes.com cheap five fingers page 1,rank 3
vibramfivetoeshoes.net cheap five fingers page 5,rank 5
vibramforshoes.com vibram five fingers page 1,rank 6
vibramfivetoeshoes.net vibram five fingers page 5,rank 10
beatsbydre.com beats by dre page 1,rank 1
justlovebeats.com beats by dre page 5,rank 7
beatsbydre.com beats by dre cheap page 2,rank 2
beatsbydre.com beats by dre cheap page 2,rank 3
beatsbydre.com beats by dre cheap page 5,rank 10
如圖:
這樣看很亂,那么我們如何才能達到上面所講 一個站后面對應多個關鍵詞的格式呢,這里我們就要用到 paiban.sh 這個小腳本了。我們把paiban.sh放在py程序的最后,當執行py程序執行完畢后,執行paiban.sh 這個paiban.sh已經加在py程序里面了,所有不需要另外執行。我這里主要看一下區別。所有在py程序里注釋了。
#sh paiban.sh #cat index.html beatsbydre.com beats by dre cheap page 2,rank 2 beats by dre cheap page 2,rank 3 beats by dre cheap page 5,rank 10 beats by dre page 1,rank 1 justlovebeats.com beats by dre page 5,rank 7 vibramfivetoeshoes.net cheap five fingers page 5,rank 5 vibram five fingers page 5,rank 10 vibramforshoes.com cheap five fingers page 1,rank 3 vibram five fingers page 1,rank 6
如圖:
這樣就能達到上面的效果了。排版也很清楚,哪個站對應哪個關鍵詞。在第幾頁,第幾位,一目了然。
我們也對paiban.sh這個腳本做一下解釋。
#vim paiban.sh #! /bin/bash sort index.html -o index.html #先把index.html文件排下序,再寫入index.html line=`wc -l index.html|awk '{print $1}'` #統計行 yuming2=`sed -n 1p index.html|awk '{print $1}'` #取第一行的域名 給yuming2 for i in `seq 2 $line` #從第二行開始了取域名 do yuming=`sed -n "$i"p index.html|awk '{print $1}'` if [ $yuming == $yuming2 ];then sed -i ""$i"s/"$yuming"//t/t/g" index.html #如果下一行域名與yuming2域名相同,就把下一行域名替換成空 else yuming2=$yuming #如果不相等,就把下一行的域名給yuming2變量 fi done
好了。這個小腳本挺好用的,老婆大人天天在用。為她減輕了不少工作量。直夸我能干。。。,哈哈。。如果有看不明白的,歡迎加QQ討論。QQ:410018348