车东很久以前写过一篇《http://www.chedong.com/tech/link_pop_check.html,如何评价一个网站的人气(Link Popularity Check)》 ,介绍通过搜索引擎的一些隐含命令评价网站的“人气”。
其实用python做到这一点很容易。
我们换一种他没有提及的方式来实现,那时候应该还没有del.ici.ous这个站点。[2:41 补充]我们还增加了对alltheweb.com这个搜索引擎的反向链接数目判断的支持。
我们提供的 getURLRank Python版本 就是这么一种概念:
一个站点的流行程度可以通过很多种方式来判断,比如通过del.ici.ous这个站点,看有多少人收藏了这个页面,据此判断。也可以通过google/alltheweb.com/msn/sogou/baidu的反向链接有多少来判断。
下面是代码,在Python 2.5c下运行成功。
[2:41 补充]上午2:30,增加了对alltheweb.com这个搜索引擎的支持,并根据pandaxiaoxi的建议增加了getattr转发器。
[20:43 补充]增加了对google.com/search.msn.com/sogou.com搜索引擎的支持,并根据limodou的建议增加了字典替换。
[23:00补充]按照limodou的建议,按照urllib.quote_plus()来编码URL。
[16:46补充]增加了对siteexplorer.search.yahoo.com搜索引擎的Inlinks检测支持).
[20061008补充]增加了对baidu收录博客情况的检测支持。
文件名为:getURLRank.py
本程序的运行结果为:
>>python geturlrank.py http://blog.sina.com.cn/m/xujinglei
enter parse_delicious function...
del.ici.ous query url:http://del.icio.us/url/check?url=http%3A%2F%2Fblog.sina.com.cn%2Fm%2Fxujinglei
get URL : http://del.icio.us/url/check?url=http%3A%2F%2Fblog.sina.com.cn%2Fm%2Fxujinglei
有多少个人收藏了你的url呢:
148
out parse_delicious function...
enter parse_google function...
google query url:http://www.google.com/search?hl=en&q=link%3Ahttp%3A%2F%2Fblog.sina.com.cn%2Fm%2Fxujinglei
google 有多少个反向链接呢:
5760
out parse_google function...
enter parse_alltheweb function...
Alltheweb query url:http://www.alltheweb.com/urlinfo?_sb_lang=any&q=http%3A%2F%2Fblog.sina.com.cn%2Fm%2Fxujinglei
get URL : http://www.alltheweb.com/urlinfo?_sb_lang=any&q=http%3A%2F%2Fblog.sina.com.cn%2Fm%2Fxujinglei
Alltheweb 有多少个反向链接呢:
1
out parse_alltheweb function...
enter parse_msn function...
msn query url:http://search.msn.com/results.aspx?FORM=QBRE&q=link%3Ahttp%3A%2F%2Fblog.sina.com.cn%2Fm%2Fxujinglei
msn 有多少个反向链接呢:
217107
out parse_msn function...
enter parse_sogou function...
sogou query url:http://www.sogou.com/web?num=10&query=link%3Ahttp%3A%2F%2Fblog.sina.com.cn%2Fm%2Fxujinglei
www.sogou.com 评分是多少呢:
66
out parse_sogou function...
enter parse_yahoo function...
yahoo siteexplorer query url:http://siteexplorer.search.yahoo.com/search?bwm=i&bwms=p&bwmf=u&fr=FP-tab-web-t500&fr2=seo-rd-se&p=http%3A%2F%2Fblog.sina.com.cn%2Fm%2Fxujinglei
siteexplorer.search.yahoo.com Inlinks是多少呢:
228334
out parse_yahoo function...
代码为:
##
## getURLRank 文档生成日期:2006.09.02
##
##(1)概述:
##模块名称: getURLRank.py
##模块说明:
## 解析用户提供的站点地址,看有多少人收藏他,以及有多少个反向链接。
## 以此来判断一个站点的流行程度。
##所在子系统: getURLRank
##
##系统总描述:
## 我们提供的 getURLRank Python版本 就是这么一种概念:
## 一个站点的流行程度可以通过很多种方式来判断,比如通过del.ici.ous这个站点,看有多少人收藏了这个页面,据此判断。
## 也可以通过google.com/alltheweb.com的反向链接有多少来判断。
#
#
##运行方式:
## python getURLRank.py -- 默认检查我的blog的各种人气指数(delicious/google/alltheweb/msn/sogou/siteexplorer.search.yahoo.com)
## python getURLRank.py delicious -- 检查我的blog被del.ici.ous收藏程度
## python getURLRank.py allthweb -- 检查我的blog被allthweb.com搜索到了多少个反向链接
## python getURLRank.py google -- 检查我的blog被google.com搜索到了多少个反向链接
## python getURLRank.py msn -- 检查我的blog被search.msn.com搜索到了多少个反向链接
## python getURLRank.py sogou -- 检查我的blog被www.sogou.com评分SogouRank是多少
## python getURLRank.py yahoo -- 检查我的blog被siteexplorer.search.yahoo.com收集到的Inlinks是多少
##
## python getURLRank.py http://blog.csdn.net -- 检查csdn blog的各种人气指数(delicious/google/alltheweb/msn/sogou)
## python getURLRank.py http://blog.csdn.net alltheweb -- 检查csdn blog被allthweb搜索到了多少个反向链接
## python getURLRank.py http://blog.csdn.net google -- 检查csdn blog被google搜索到了多少个反向链接
## python getURLRank.py http://blog.csdn.net msn -- 检查csdn blog被search.msn.com搜索到了多少个反向链接
## python getURLRank.py http://blog.csdn.net sogou -- 检查csdn blog的SogouRank是多少
##
##(2)历史记录:
##创建人: zhengyun_ustc(2006年9月3日, 上午1:00)
##修改人: zhengyun_ustc(2006年9月3日, 上午2:30,增加了对alltheweb.com这个搜索引擎的支持,并根据pandaxiaoxi的建议增加了getattr转发器)
##修改人: zhengyun_ustc(2006年9月3日, 下午4:30,增加了对google.com和search.msn.com搜索引擎的支持)
##修改人: zhengyun_ustc(2006年9月3日, 下午5:10,增加了对www.sogou.com搜索引擎的SogouRank检测支持)
##修改人: zhengyun_ustc(2006年9月5日, 下午2:10,增加了对siteexplorer.search.yahoo.com搜索引擎的Inlinks检测支持)
##联系我: Google Talk >> zhengyun(at)gmail.com
##Blogs: http://blog.csdn.net/zhengyun_ustc/
##(3)版权声明:
##zhengyun_ustc这个python版本的 getURLRank ,代码您可以借鉴,但不得用于商业用途,除非得到zhengyun_ustc的授权。
#
from sgmllib import SGMLParser
import os,sys,re
import socket
import httplib
import urllib
import urllib2
from xml.dom import minidom
name = 'zhengyun'
google_email = 'your@gmail.com'
google_password = 'pass'
# 可以将要替换的东西写成一个list或字典来对应,然后通过循环进行替换
#aReplaceURL = [('://','%3A%2F%2F'), ('/','%2F')]
# google声明:“未经 Google 事先明确许可,不得将任何形式的自动查询发到 Google 系统。请注意,“自动查询”包括通过使用软件向 Google 发送查询来确定搜索不同内容时网站的 Google 排名”
# 所以如果要用Google的某些服务,比如日历,我们只能事先登录。
# 但是对于查询,是不用登录的。
def initGoogleLogin(email, passwd):
params = urllib.urlencode({'Email': email,
'Passwd': passwd,
'service': 'cl',
'source': 'upbylunch-googie-0'})
headers = {"Content-type": "application/x-www-form-urlencoded"}
print 'enter initGoogleLogin function...'
conn = httplib.HTTPSConnection("www.google.com")
conn.request("POST", "/accounts/ClientLogin", params, headers)
response = conn.getresponse()
data = response.read()
# If the login fails Google returns 403
if response.status == 403:
google_auth = None
else:
google_auth = data.splitlines()[2].split('=')[1]
print 'google_auth=' + google_auth
conn.close
return google_auth
class GoogleClRedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_301(self, req, fp, code, msg, hdrs):
result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, hdrs)
result.status = code
return result
def http_error_201(self, req, fp, code, msg, hdrs):
return 'Success'
def http_error_302(self, req, fp, code, msg, hdrs):
result = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, hdrs)
result.status = code
return hdrs.dict['location']
# 获取web页面内容并返回
def getURLContent(url):
print "get URL : %s" % url
f = urllib.urlopen(url)
data = f.read()
f.close()
return data
#检查del.ici.ous站点对你的收藏情况的函数
def parse_delicious(url):
print 'enter parse_delicious function...'
# 这是delicious网站检查连接是否被收藏的链接前缀
delicousPrefixURL = 'http://del.icio.us/url/check?url='
#for i, j in aReplaceURL:
# postData = url.replace(i, j)
postData = delicousPrefixURL + urllib.quote_plus(url)
print 'del.ici.ous query url:' + postData
# 下面这个data元素是delicious网站检查结果的HTML
data = getURLContent(postData)
#print data
# 下面我们要从中通过正则表达式
# urls*hass*beens*saveds*by(s*?[^p]*s*)people
# 来解析出到底有多少人收藏了:
pParsere = re.compile('url/s*has/s*been/s*saved/s*by(/s*?[^p]*/s*)people')
#print '输出正则表达式对象:'
#print pParsere
# 正式开始解析,此处使用pParsere.match(data)是不行的;只能使用pParsere.search(data)
matchDelicious = pParsere.search(data)
if(matchDelicious):
#print '有多少个人收藏?是否解析成功呢?:'
#print matchDelicious
print '有多少个人收藏了你的url呢:'
# 如果这里输出matchDelicious.group(0),那么将会是整个输出,如“url has been saved by 2 people”
# 而如果是group(1),则是正确的数字:2
print matchDelicious.group(1).replace(',','')
else:
# 那我们只有通过正则表达式
# Theres*iss*nos*del.icio.uss*historys*fors*thiss*url
# 来解析出是不是没有人收藏了:
pParsere = re.compile('There/s*is/s*no/s*del.icio.us/s*history/s*for/s*this/s*url')
#print '输出正则表达式对象:'
#print pParsere
# 正式开始解析,此处使用pParsere.match(data)是不行的;只能使用pParsere.search(data)
matchDelicious = pParsere.search(data)
if(matchDelicious):
print '是不是没有人收藏?是否解析成功呢?:'
print matchDelicious
print '没有人收藏这个url!'
print 'out parse_delicious function...'
#检查Alltheweb站点对你的反向链接情况的函数
def parse_alltheweb(url):
print 'enter parse_alltheweb function...'
# 这是Alltheweb网站检查是否有反向链接的前缀
# _sb_lang=any这个参数极其的重要!
# 如果没有这个参数,那么alltheweb仅仅在english里搜索,那么势必会造成失真,比如我的blog只有12个结果;
# 只有发起请求的时候就列出“_sb_lang=any&”参数,才可以在全部语言中搜索,这样我的blog就有212个结果了。
AllthewebPrefixURL = 'http://www.alltheweb.com/urlinfo?_sb_lang=any&q='
#for i, j in aReplaceURL:
# postData = url.replace(i, j)
postData = AllthewebPrefixURL + urllib.quote_plus(url)
print 'Alltheweb query url:' + postData
# 下面这个data元素是Alltheweb网站检查结果的HTML
data = getURLContent(postData)
#print data
# 下面我们要从中通过正则表达式
#
# 来解析出到底有多少个反向链接:
pParsere = re.compile(r'
#print '输出正则表达式对象:'
#print pParsere
# 正式开始解析,此处使用pParsere.match(data)是不行的;只能使用pParsere.search(data)
matchAlltheweb = pParsere.search(data)
if(matchAlltheweb):
#print '有多少个反向链接?是否解析成功呢?:'
#print matchAlltheweb
print 'Alltheweb 有多少个反向链接呢:'
# 而如果是group(1),则是正确的数字:212
print matchAlltheweb.group(1).replace(',','')
else:
# 那我们只有通过正则表达式
# Nos*Webs*pagess*founds*thats*matchs*yours*query
# 来解析出是不是没有反向链接:
pParsere = re.compile('No/s*Web/s*pages/s*found/s*that/s*match/s*your/s*query')
#print '输出正则表达式对象:'
#print pParsere
# 正式开始解析,此处使用pParsere.match(data)是不行的;只能使用pParsere.search(data)
matchAlltheweb = pParsere.search(data)
if(matchAlltheweb):
print '是不是没有反向链接?:'
print matchAlltheweb
print '这个url没有反向链接!'
print 'out parse_alltheweb function...'
# 解析google网页内容,得到有多少个连接
def parseGoogleResult(page):
m = re.search('(? of about )([0-9]|,)+',page)
return m.group(0).replace(',','')
#检查 google 站点对你的反向链接情况的函数
def parse_google(url):
print 'enter parse_google function...'
# 首先模拟google登录,获取验证信息:
#google_auth = initGoogleLogin(google_email, google_password)
# 这是 google 网站检查是否有反向链接的前缀