This article mainly introduces how to obtain the PR and Baidu weights of a website using Python. This article uses the parameter passing method to request the webmaster tool and Google tool to obtain the PR and Baidu weights, for more information, see the simple code that I wrote in the requests Library next time, we can also use it to obtain the PR and Baidu weights of our website. The principle is similar. Finally, we can write a loop to query the website information in batches.
Let's talk about GooglePR, the full name of PageRank. It is officially provided by Google to evaluate the SEO rating of a website, which should be familiar to everyone. Since it is officially provided, of course there is an official interface to get it. Here we use the official interface to get Google PR.
The code is as follows:
Maid = "Mining PageRank is against google's terms of service. Y \
Es, I'm talking to you, scammer ."
Def google_hash (value ):
Magic = 0x1020345
For I in xrange (len (value )):
Magic ^ = ord (maid [I % len (maid)]) ^ ord (value [I])
Magic = (magic> 23 | magic <9) & 0 xFFFFFFFF
Return "8% 08x" % (magic)
Def getPR (www ):
Try:
Url = 'http: // toolbarqueries.google.com/tbr? '\
'Client = navclient-auto & ch = % s & features = Rank & q = info: % s' % (google_hash (www), www)
Response = requests. get (url)
Rex = re. search (r '(.*? :.*? :) (\ D +) ', response. text)
Return rex. group (2)
Except t:
Return None
Usage: pass in the domain name and return the prvalue
The google_hash function is just an algorithm that computes a domain name that is similar to a hash value and returns it. We can ignore how it is implemented. we mainly look at the getPR function. Our official google interface is this: http://toolbarqueries.google.com/tbr? Client = navclient-auto & ch = {HASH} & features = Rank & q = info: {domain name}
{HASH} Here we use the google_hash () function to pass in the domain name and return its corresponding HASH value. For example, we leave the song domain www.leavesongs.com, its Google HASH is 8b1e6ad00, so the construction of the consulting site is: http://toolbarqueries.google.com/tbr? Client = navclient-auto & ch = 8b1e6ad00 & features = Rank & q = info: www.leavesongs.com
Access it to obtain Rank_1: 1: 0. The number following the second quotation mark is PR, because my website does not have PR, so PR is 0.
Therefore, we use requests. get () to access our constructed URL, obtain the result like Rank_1: 1: 0, and get the prvalue 0 through regular expressions or other methods.
The above is the execution process of the getPR function. Let's look at the process of obtaining Baidu weights.
Baidu's weight is not a standard officially provided by Baidu. it is a value calculated by some third-party websites, so there is no interface like PR. Therefore, we need to capture information from these third-party websites. The following is a function for obtaining Baidu weights:
The code is as follows:
Def getBR (www ):
Try:
Url = 'http: // mytool.chinaz.com/baidusort.aspx? Host = % s & sortType = 0' % (www ,)
Response = requests. get (url)
Data = response. text
Rex = re. search (r '(
. + ?) (\ D *?) () ', Data, re. I)
Return rex. group (2)
Except t:
Return None
The method is also used to pass in the domain name and return the weight value.
I crawled is webmaster tools a weight consulting page: http://mytool.chinaz.com/baidusort.aspx? Host = {domain name} & sortType = 0
My regular expression is :(
. + ?) (\ D *?) (), You can check the source code to see how to write the regular expression.
Let's get the PR and weight of these websites in batches:
View the result directly:
If a single process is scanned, the scanning speed will be slightly slow. if you open 10 or 20 threads, it will be faster to obtain them in batches.