背景
在做seo的过程中,不可避免的先要分析关键词,获取关键词,入门的工具应该就是百度的凤巢系统了,本篇文章介绍了如果根据关键词获取周搜索量以及点击量等数据。
GUI实例
一下是操作界面
代码
话不多说,直接上代码
def url_data(self, key, config, cookie, shibai=3):
headers = {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8,en;q=0.6',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': cookie,
# 在下面config这个变量值下面的cookie中粘贴进抓包后的cookie,这里不要动。
'Host': 'fengchao.baidu.com',
'Origin': 'https://fengchao.baidu.com',
'Referer': 'https://fengchao.baidu.com/fc/manage/new/user/%s/mt/all/keyword?currentKrPage=kr_recommend' %
config['userid'],
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
}
from_data = {
'params': '{"keyWordRecommendFilter":{"device":0,"positiveWords":[],"negativeWords":[],"regionExtend":true,"removeDuplicate":true,"keywordRecommendReasons":[],"searchRegions":"9999999"},"source":"web","queryType":0,"querys":["%s"],"entryMessage":"kr_station"}' % key,
'path': 'lightning/GET/KeywordSuggestService/getKeywordRecommendPassive',
'userid': config['userid'],
'token': config['token'],
'eventId': config['eventId'],
'reqid': config['reqid']
}
qurl = "https://fengchao.baidu.com/hairuo/request.ajax?path=lightning/GET/KeywordSuggestService/getKeywordRecommendPassive&reqid=%s" % \
config['reqid']
try:
tunnel = "tps129.kdlapi.com:15818"
proxies = {
"http": "http://%(proxy)s/" % {"proxy": tunnel},
"https": "http://%(proxy)s/" % {"proxy": tunnel}
}
whtml = requests.post(qurl, headers=headers, data=from_data, proxies=proxies)
# whtml = requests.post(qurl, headers=headers, data=from_data)
# time.sleep(.5)
except requests.exceptions.RequestException as e:
print(e)
resultitem = {}
erry = str(self.process_num) + " 请求三次都是错误!"
time.sleep(1)
if shibai > 0:
return self.url_data(key, config, cookie, shibai - 1)
else:
whtml.encoding = "utf-8"
try:
resultitem = whtml.json()
except ValueError:
resultitem = {}
erry = "获取不到json数据,可能是被封了吧,谁知道呢?"
else:
erry = None
return resultitem, erry
上面的方法是调用的方法,里面使用到了代理,不使用注掉即可
接下来的代码就是需要使用者自己配置的部分了。
config = {
'userid': '29975**',
'token': '166106***',
'eventId': '4b534c**-adb7-4ced-**-159668199560',
'reqid': '4b534c**-c6b0-4e49-**-159668199560'
}
cookie = 'cookie: ***PSTM=1582795 BAIDUID=00D49E396AF65D1***'
这部分数据和下面的cookie,直接开浏览器抓包就能看到相应数据。复制黏贴到相应位置