从谷歌搜索精确短语

2024-09-29 19:28:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用python从google搜索引擎中搜索一些短语。我给输入字符串'DVD player'并用Google搜索包含这两个词的页面,并用DVD+player术语形式化搜索。但我想搜索准确的短语。我试图用"\"DVD plyar\""替换这个词,这样谷歌就可以搜索到"DVD player",但似乎没有用。在

以下是从其他问题中选择的代码。在

import json    
import urllib.request    
def showsome(searchfor):    
    query = urllib.parse.urlencode({'q': searchfor})    
    url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query

    search_response = urllib.request.urlopen(url)    
    search_results = search_response.read().decode('utf8')    
    results = json.loads(search_results)    
    data = results['responseData']    
    print ('Total results: %s' % data['cursor']['estimatedResultCount'])

    hits = data['results']    
    print ('Top %d hits:' % len(hits))    
    for h in hits: print (' ', h['url'])

    print ('For more results, see %s' % data['cursor']['moreResultsUrl'])    

showsome("\"DVD player\"")

Tags: importjsonurlsearchdatarequesturllibresults

热门问题