Python批量查询关键词是否是指数词
简单的查询一个关键词是否为指数词,暂不能查询指数,过段时间研究下
# -*- 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
推荐阅读