前段时间为了查找自己的老王python的关键词排名怎么样,特意用python写了一个查找网页关键词排名的程序,感觉效果还不错。特别是查找关键词排名靠后的网页来说非常的方便,不用自己手动的去一个个的翻页,废话不说了,赶快上代码。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#@author:wl
#@qq:280026798@qq.com
#@description:检测给定的关键字在百度上的排名
import sys
import urllib ,urllib2
import re
def baidu(w,pn):
'''返回当前页的内容'''
url= "http://www.baidu.com/s?"
values = {
"w":w.encode('gbk','ignore'),
"pn":pn
}
data = urllib.urlencode(values)
newurl = url + data
response = urllib2.urlopen(newurl)
the_page = response.read()
return the_page
def ana(data,mysite,pn):
'''检测关键字的位置'''
o = re.compile(r'href="(.+?)"')
f = o.findall(data)
line = pn
for ff in f:
ff = ff.strip()
if not re.search("^s\?",ff) and re.search("^http:\/\/",ff) and not re.search('baidu.com',ff):
if re.search(mysite,ff):
print "* " ,line ,ff
return True
else:
print line,ff
line = line + 1
continue
if __name__ == "__main__":
mysite = sys.argv[2]
pn = 1
while True:
keyword = sys.argv[1].decode('gbk')
data = baidu(keyword,pn)
checkflag = ana(data,mysite,pn)
if not checkflag:
pn = pn + 10
print "page %s" % str(int(pn)/10)
else:
print 'found:%s' % (mysite)
break
else:
print 'not found:%s' % (mysite)
# -*- coding: utf-8 -*-
#@author:wl
#@qq:280026798@qq.com
#@description:检测给定的关键字在百度上的排名
import sys
import urllib ,urllib2
import re
def baidu(w,pn):
'''返回当前页的内容'''
url= "http://www.baidu.com/s?"
values = {
"w":w.encode('gbk','ignore'),
"pn":pn
}
data = urllib.urlencode(values)
newurl = url + data
response = urllib2.urlopen(newurl)
the_page = response.read()
return the_page
def ana(data,mysite,pn):
'''检测关键字的位置'''
o = re.compile(r'href="(.+?)"')
f = o.findall(data)
line = pn
for ff in f:
ff = ff.strip()
if not re.search("^s\?",ff) and re.search("^http:\/\/",ff) and not re.search('baidu.com',ff):
if re.search(mysite,ff):
print "* " ,line ,ff
return True
else:
print line,ff
line = line + 1
continue
if __name__ == "__main__":
mysite = sys.argv[2]
pn = 1
while True:
keyword = sys.argv[1].decode('gbk')
data = baidu(keyword,pn)
checkflag = ana(data,mysite,pn)
if not checkflag:
pn = pn + 10
print "page %s" % str(int(pn)/10)
else:
print 'found:%s' % (mysite)
break
else:
print 'not found:%s' % (mysite)
用法:
先保存成python文件,然后要运行的话,可以按照下面的步骤。
python xxx.py 关键词 网址
就可以查出关键词在百度搜索列表的具体位置了。
文章链接:http://www.cnpythoner.com/post/webkeyword.html 转载请保留,谢谢!