Ranking of search keywords in Baidu in Python

Source: Internet
Author: User

Some time ago, in order to find the keyword ranking of Lao Wang python, I wrote a keyword ranking for searching web pages using python.Program, It seems that the effect is not bad. In particular, it is very convenient to search for webpages with the keyword ranking back. You don't have to manually flip pages one by one.Code.

#! /Usr/bin/ENV Python
#-*-Coding: UTF-8 -*-
# @ Author: WL
# @ Qq: 280026798@qq.com
# @ Description: Check the ranking of a given keyword on Baidu

Import sys
Import urllib, urllib2
Import re

DefBaidu (W, PN ):
'''Return the content of the current page '''
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 ()
ReturnThe_page

Def Ana (data, mysite, PN ):
'''Location of the detection keyword '''
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) usage: first save it as a python file, and then run it, follow the steps below. The Python XXX. py keyword URL can be used to locate the keyword in the baidu search list.

ArticleLink: http://www.cnpythoner.com/post/webkeyword.html reprint please keep, thank you!

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.