海外翻墙免费加速器:[入口]
分析关键词在百度的相关结果数
有利于了解关键词在百度的竞争程度,比如一个很热门的词,那么它的相关结果数必然很高,因为大家都想做此关键词的排名,对于做SEO,这类词是不适合我们竞争的,因为这类关键词基本需求已经处于饱和状态,想要获取排名,需要的周期和代价会很大。
python脚本如下
#coding:utf-8
#author:
import requests #打开
import time,random #控制时间
import sys #专门乱码的
from bs4 import BeautifulSoup as bs #把html结构化
reload(sys)
sys.setdefaultencoding(‘utf-8’)
headers={
‘User-Agent’:’Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36′,
‘Cookie’:’BAIDUID=7C056061DB94F357117D8DB0C8404E70:FG=1; BIDUPSID=7C056061DB94F357117D8DB0C8404E70; PSTM=1492480594; pgv_pvi=2421839872; BDUSS=1Ed0hsZkZvd3ZUTHhaamlwU2hQdUJZOW5uMlFCNU54M0REbHdBdkpRTkJ3eDVaSVFBQUFBJCQAAAAAAAAAAAEAAABoU1kkzb7Fo7~GvLwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEE291hBNvdYQS; BD_CK_SAM=1; PSINO=3; H_PS_PSSID=1420_21127_17001_20927; BD_UPN=12314353; sug=3; sugstore=0; ORIGIN=0; bdime=0; H_PS_645EC=725aOa7v%2F0jJ7JlDps7YdE0TcC3sAd5Zzgmyw1s7mW3N%2BZxKBwDVWAyY%2FE0; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598′,
}
with open (‘url.txt’) as f:
for link in f:
ID,keyword=link.strip().split(‘;’)
url=’https://www.baidu.com/s?wd=’+keyword ####拼写百度url
print url
cont = requests.get(url,timeout=120,headers=headers).content
soup = bs(cont, “html.parser”)
infos = soup.find(‘div’,{‘class’:”nums”}) ##利用soup里面的find方法获取class为nums的标签
search_tool=infos.div.extract() #### 排除获取内容的其他div
num=infos.text
num1=num.replace(“百度为您找到相关结果约”,””) ###替换
txt=open(‘num.txt’,’a’)
txt.write(ID+’\t’+keyword+’\t’+num1+’\n’)
txt.close()
time.sleep(0.3)
未经允许不得转载:陈海飞博客 » python批量获取百度关键词相关结果数