Python Query Baidu SEO information

Source: Internet
Author: User
A simple python query Baidu keyword ranking function, features:

1. UA Random

2, the operation is simple and convenient, direct getrank (keywords, domain name) can be

3, code conversion. There should be no problem with coding.

4, the result is rich. Not only rankings, but also search results title,url, snapshot time, in line with SEO requirements


Disadvantages:

Single thread, slow speed

#coding =utf-8 Import Requestsimport beautifulsoupimport reimport random def Decodeanyword (W): Try:w.decode (' u Tf-8 ') except:w = W.decode (' gb2312 ') else:w = W.decode (' Utf-8 ') Return w def createurl (Checkword ): #create Baidu URL with search words Checkword = Checkword.strip () Checkword = Checkword.replace (', ' + '). Repla CE (' \ n ', ') Baiduurl = ' http://www.baidu.com/s?wd=%s&rn=100 '% checkword return baiduurl def getcontent (Baiduu RL): #get The content of the serp ualist = [' mozilla/4.0+ (compatible;+msie+6.0;+windows+nt+5.1;+sv1;+.net+clr+1.1.432 2;+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) ' headers = {' User-agent ': Random.choice (ualist)} iplist = [' 202.43.188.13:8080 ', ' 80.243. 185.168:1177 ', ' 218.108.85.59:81 '] proxies = {' http ': ' http://%s '% Random.choice (iplist)} r = Requests.get (Baidu URL, headers = headers, proxies = proxies) return r.content def getlasturl (rawurl): #get final URL while there ' re Redi Rects r = Requests.get (Rawurl) return R.url def getatext (atext): #get the text with and Pat = Re.compile (R '    (.*?)') Match = Pat.findall (atext) PureText = Match[0].replace ('' , '). Replace ('', ') return PureText def getcachedate (t): #get The date of the cache Pat = Re.compile (R '.*? (\d{4}-\d{1,2}-\d{1,2})Match = Pat.findall (t) cachedate = match[0] return cachedate def getrank (checkword, domain): #main line Ch  Eckword = Checkword.replace (' \ n ', ') Checkword = Decodeanyword (checkword) Baiduurl = CreateURL (checkword) cont =    GetContent (baiduurl) soup = beautifulsoup.beautifulsoup (cont) results = soup.findall (' table ', {' class ': ' Result '}) #find all results in this page for result in Results:checkdata = Unicode (Result.find (' span ', {' class ': ' G '}) If Re.compile (R ' ^[^/]*%s.*? '%domain). Match (Checkdata): #改正则 Nowrank = result[' id ') #get the rank if Match the domain info reslink = result.find (' h3 '). A resurl = reslink[' href '] Domainurl = Getlasturl (Resurl) #get the target URL restitle = Getatext (Unicode (Reslink)) #get The title of the target Page Rescache = result.find (' span ', {' class ': ' G '}) Cachedate = Getcachedate (Unicode (rescache)) #g ET the cache date of the Target page res = U '%s, first%s name,%s,%s,%s '% (Checkword, Nowrank, Restitle, Cachedate, Domainurl) Return Res.encode (' gb2312 ') break Else:return ' >100 ' domain = ' www.douban.com ' #set the domain W      Hich want to search. f = open (' R.txt ') for W in F.readlines (): Print GetRank (w, domain) F.close ()
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.