需要在墙的外面使用。
方式一:
代码:
import requests import json # from fake_useragent import UserAgent keyword = "dog collars" keyword.replace(" ", "+") url = "http://suggestqueries.google.com/complete/search?output=firefox&q=" + keyword # ua = UserAgent() # headers = {"user-agent": ua.chrome} # headers = {"user-agent": ua.chrome} response = requests.get(url, verify=False) suggestions = json.loads(response.text) for word in suggestions[1]: print(word)
亲测可用。
在本地配合蜂巢也可以使用。
参考:https://importsem.com/query-google-suggestions-api-with-python/
二、方式二
这个支持其他语言
http://suggestqueries.google.com/complete/search?output=toolbar&hl=zh&q=%E4%B8%AD%E5%9B%BD
代码:
import requests from bs4 import BeautifulSoup from random import choice UserAgentList = ['Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36', 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'] def RandomUserAgent(): UserAgent = choice(UserAgentList) UserAgent = {'User-Agent':UserAgent} return UserAgent def get_suggestion(suggestion): r = requests.get('{}{}'.format('http://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=',suggestion.replace(' ','+')),headers=RandomUserAgent()) soup = BeautifulSoup(r.text,'lxml-xml') suggestions = soup.find_all('suggestion') suggestion_list = [] for suggest in suggestions: suggest = suggest['data'] suggestion_list.append(suggest) return suggestion_list a = get_suggestion("china") print(a)
参考:https://github.com/dengwen168/google_keyword_suggest/blob/master/google_suggest.py