python查询seo信息的简单示例

前端之家收集整理的这篇文章主要介绍了python查询seo信息的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对python这个高级语言感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!

一个简单的python查询百度关键词排名的函数,特点:

1、UA随机

2、操作简单方便,直接getRank(关键词,域名)就可以了

3、编码转化。编码方面应该没啥问题了。

4、结果丰富。不仅有排名,还有搜索结果的title,URL,快照时间,符合SEO需求

 

缺点:

单线程,速度慢

  1. # @param python查询百度SEO信息
  2. # @author 编程之家 jb51.cc|www.jb51.cc
  3. #coding=utf-8
  4. import requests
  5. import BeautifulSoup
  6. import re
  7. import random
  8. def decodeAnyWord(w):
  9. try:
  10. w.decode('utf-8')
  11. except:
  12. w = w.decode('gb2312')
  13. else:
  14. w = w.decode('utf-8')
  15. return w
  16. def createURL(checkWord): #create baidu URL with search words
  17. checkWord = checkWord.strip()
  18. checkWord = checkWord.replace(' ','+').replace('\n','')
  19. baiduURL = 'http://www.baidu.com/s?wd=%s&rn=100' % checkWord
  20. return baiduURL
  21. def getContent(baiduURL): #get the content of the serp
  22. uaList = ['Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1;+.NET+CLR+1.1.4322;+TencentTraveler)','Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1;+.NET+CLR+2.0.50727;+.NET+CLR+3.0.4506.2152;+.NET+CLR+3.5.30729)','Mozilla/5.0+(Windows+NT+5.1)+AppleWebKit/537.1+(KHTML,+like+Gecko)+Chrome/21.0.1180.89+Safari/537.1','Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1)','Mozilla/5.0+(Windows+NT+6.1;+rv:11.0)+Gecko/20100101+Firefox/11.0','Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+5.1;+Trident/4.0;+SV1)','Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+5.1;+Trident/4.0;+GTB7.1;+.NET+CLR+2.0.50727)','Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+5.1;+Trident/4.0;+KB974489)']
  23. headers = {'User-Agent': random.choice(uaList)}
  24. ipList = ['202.43.188.13:8080','80.243.185.168:1177','218.108.85.59:81']
  25. proxies = {'http': 'http://%s' % random.choice(ipList)}
  26. r = requests.get(baiduURL,headers = headers,proxies = proxies)
  27. return r.content
  28. def getLastURL(rawurl): #get final URL while there're redirects
  29. r = requests.get(rawurl)
  30. return r.url
  31. def getAtext(atext): #get the text with <a> and </a>
  32. pat = re.compile(r'<a .*?>(.*?)</a>')
  33. match = pat.findall(atext)
  34. pureText = match[0].replace('<em>','').replace('</em>','')
  35. return pureText
  36. def getCacheDate(t): #get the date of cache
  37. pat = re.compile(r'<span class="g">.*?(\d{4}-\d{1,2}-\d{1,2}) </span>')
  38. match = pat.findall(t)
  39. cacheDate = match[0]
  40. return cacheDate
  41. def getRank(checkWord,domain): #main line
  42. checkWord = checkWord.replace('\n','')
  43. checkWord = decodeAnyWord(checkWord)
  44. baiduURL = createURL(checkWord)
  45. cont = getContent(baiduURL)
  46. soup = BeautifulSoup.BeautifulSoup(cont)
  47. results = soup.findAll('table',{'class': 'result'}) #find all results in this page
  48. for result in results:
  49. checkData = unicode(result.find('span',{'class': 'g'}))
  50. if re.compile(r'^[^/]*%s.*?' %domain).match(checkData): #改正则
  51. nowRank = result['id'] #get the rank if match the domain info
  52. resLink = result.find('h3').a
  53. resURL = resLink['href']
  54. domainURL = getLastURL(resURL) #get the target URL
  55. resTitle = getAtext(unicode(resLink)) #get the title of the target page
  56. rescache = result.find('span',{'class': 'g'})
  57. cacheDate = getCacheDate(unicode(rescache)) #get the cache date of the target page
  58. res = u'%s,第%s名,%s,%s' % (checkWord,nowRank,resTitle,cacheDate,domainURL)
  59. return res.encode('gb2312')
  60. break
  61. else:
  62. return '>100'
  63. domain = 'www.douban.com' #set the domain which you want to search.
  64. f = open('r.txt')
  65. for w in f.readlines():
  66. print getRank(w,domain)
  67. f.close()
  68. # End www.jb51.cc

猜你在找的Python相关文章