簡單的查詢一個關(guān)鍵詞是否為指數(shù)詞,暫不能查詢指數(shù),過段時間研究下
# -*- coding: utf-8 -*-
import urllib2
import urllib
import cookielib
import re
import bs4
URL_BAIDU_INDEX = u'';
# 也可以用這個
URL_BAIDU_TOKEN = '';
URL_BAIDU_LOGIN = '';
#設(shè)置用戶名、密碼
username = '';
password = '';
#設(shè)置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');
#構(gòu)造登錄請求參數(shù),該請求數(shù)據(jù)是通過抓包獲得,對應(yīng)請求
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);
#發(fā)送登錄請求
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);
#獲取指數(shù)頁面
keylist = open('key.txt','r') #查詢關(guān)鍵詞的文件
i = '如要查看相關(guān)數(shù)據(jù)'
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,'不是指數(shù)關(guān)鍵詞'
else:
print key,'指數(shù)關(guān)鍵詞'
喜歡python,SEO的同學(xué)可以加QQ群472779600
申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點子。點擊此處,共同探討創(chuàng)業(yè)新機遇!