简单的查询一个关键词是否为指数词,暂不能查询指数,过段时间研究下
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
import re
import bs4
URL_BAIDU_INDEX = u'';
# 也可以用这个
URL_BAIDU_TOKEN = '';
URL_BAIDU_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');
#构造登录请求参数,该请求数据是通过抓包获得,对应请求
postData = {
'username' : username,
'password' : password,
'u' : '',
'tpl' : 'pp',
'token' : tokenVal,
'staticpage' : '',
'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 = '{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
申请创业报道,分享创业好点子。点击此处,共同探讨创业新机遇!