Python批量采集百度下拉框推荐词教程

人生苦短,我用Python,从入门到放弃,在到学会装X,这里是一份装X指南,教你如何用Python批量采集百度下拉框推荐词教程。

 

首先,新建文件名,保存为py后缀,将下面代码复制到sublime编辑中:

#encoding=utf-8
import requests,json,urllib

for word in open('kws.txt',encoding='utf-8'):
  url='https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=%s&sugmode=2&json=1&p=3&sid=1427_21091_21673_22581&req=2' % urllib.parse.quote_plus(word)
  html=requests.get(url).text

  html=html.replace('window.baidu.sug(','')

  html=html.replace(');','')
  #print (html)
  html=json.loads(html)
  #print (html['s'])
  for i in html['s']:
    print (i)

 

采集结果如图:

下拉词采集

 

复制以下代码,将采集关键词打印到word文档:

#encoding=utf-8
import requests,json,urllib
opencsv=open('word.csv','a+')
for word in open('kws.txt',encoding='utf-8'):
  url='https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=%s&sugmode=2&json=1&p=3&sid=1427_21091_21673_22581&req=2' % urllib.parse.quote_plus(word)
  html=requests.get(url).text

  html=html.replace('window.baidu.sug(','')

  html=html.replace(');','')
  #print (html)
  html=json.loads(html)
  #print (html['s'])
  for i in html['s']:
    print (i)
    opencsv.write('%s\n'%i)

 

打开word文档,如图:

关键词word文档

 

以上就是用Python批量采集百度下拉推荐词的完整教程。

赞赏

微信赞赏支付宝赞赏

相关文章