运行效果:
代码:
import requests import json def get_sug(word): url = 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=%s&sugmode=2&json=1&p=3&sid=1427_21091_21673_22581&req=2&pbs=%%E5%%BF%%AB%%E6%%89%%8B&csor=2&pwd=%%E5%%BF%%AB%%E6%%89%%8B&cb=jQuery11020924966752020363_1498055470768&_=1498055470781' % word r = requests.get(url, verify=False) # 请求API接口,取消了HTTPS验证 cont = r.content # 获取返回的内容 res = cont[41: -2].decode('gbk') # 只取返回结果中json格式一段,并且解码为unicode res_json = json.loads(res) # json格式转换 return res_json['s'] # 返回关键词列表 print( 'n'.join(get_sug('植物提取物')))