愚愚学园
www.SciFans.net温馨提示:简单的查询一个关键词是否为指数词,暂不能查询指数,过段时间研究下
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
import re
import bs4
URL_BAIDU_INDEX = u'http://www.baidu.com/';
#
https://passport.baidu.com/v2/api/?getapiclass=logintpl=mntangram=true 也可以用这个
URL_BAIDU_TOKEN = 'https://passport.baidu.com/v2/api/?getapitpl=ppapiver=v3class=login';
URL_BAIDU_LOGIN = 'https://passport.baidu.com/v2/api/?login';
#设置用户名、密*****
username = '';
password = '';
#设置cookie,这里cookiejar可自动管理,无需手动指定
cj = cookielib.CookieJar();
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj));
urllib2.install_opener(opener);
reqReturn = urllib2.urlopen(URL_BAIDU_INDEX);
#获取token,
tokenReturn = urllib2.urlopen(URL_BAIDU_TOKEN);
matchVal = re.search(u'"token" : "(?P.*?)"',tokenReturn.read());
tokenVal = matchVal.group('tokenVal');
#构造登录请求参数,该请求数据是通过抓包获得,对应
https://passport.baidu.com/v2/api/?login请求
postData = {
'username' : username,
'password' : password,
'u' : 'https://passport.baidu.com/',
'tpl' : 'pp',
'token' : tokenVal,
'staticpage' : 'https://passport.baidu.com/static/passpc-account/html/v3Jump.html',
'isPhone' : 'false',
'charset' : 'UTF-8',
'callback' : 'parent.bd__pcbs__ra48vi'
};
postData = urllib.urlencode(postData);
#发送登录请求
loginRequest = urllib2.Request(URL_BAIDU_LOGIN,postData);
loginRequest.add_header('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
loginRequest.add_header('Accept-Encoding','gzip,deflate,sdch');
loginRequest.add_header('Accept-Language','zh-CN,zh;q=0.8');
loginRequest.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36');
loginRequest.add_header('Content-Type','application/x-www-form-urlencoded');
sendPost = urllib2.urlopen(loginRequest);
#获取指数页面
keylist = open('key.txt','r') #查询关键词的文件
i = '如要查看相关数据'
for key in keylist:
key = key.strip('\n')
teibaUrl = 'http://index.baidu.com/?tpl=trendword={0}'.format(key)
content = urllib2.urlopen(teibaUrl).read();
content = content.decode('gbk').encode('utf8');
if i in content:
print key,'不是指数关键词'
else:
print key,'指数关键词'
喜欢python,SEO的同学可以加QQ群472779600