當(dāng)前位置:首頁(yè) >  站長(zhǎng) >  搜索優(yōu)化 >  正文

Python批量查詢關(guān)鍵詞是否是指數(shù)詞

 2016-01-24 13:45  來(lái)源: 用戶投稿   我來(lái)投稿 撤稿糾錯(cuò)

  域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過(guò)

簡(jiǎn)單的查詢一個(gè)關(guān)鍵詞是否為指數(shù)詞,暫不能查詢指數(shù),過(guò)段時(shí)間研究下

# -*- coding: utf-8 -*-

import urllib2

import urllib

import cookielib

import re

import bs4

URL_BAIDU_INDEX = u'';

# 也可以用這個(gè)

URL_BAIDU_TOKEN = '';

URL_BAIDU_LOGIN = '';

#設(shè)置用戶名、密碼

username = '';

password = '';

#設(shè)置cookie,這里cookiejar可自動(dòng)管理,無(wú)需手動(dòng)指定

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)造登錄請(qǐng)求參數(shù),該請(qǐng)求數(shù)據(jù)是通過(guò)抓包獲得,對(duì)應(yīng)請(qǐ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ā)送登錄請(qǐng)求

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ù)頁(yè)面

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

申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

相關(guān)文章

熱門(mén)排行

信息推薦