通过Python返回谷歌搜索结果数量
我想要快速获取某个关键词在谷歌搜索中返回的结果数量,并且尽量少用第三方库。我已经考虑过使用xgoogle这个工具。
2 个回答
-1
你可以使用 urllib 来下载网站的内容,然后用 HTMLParser 来提取 <div id="resultStats">....</div>
里面的值。下面是一个例子:
2
如果你搜索一些模糊的词,比如“汽车”,那么data
的内容大概会是这样的。注意,这个内容并不长;你只会得到前面几个结果,还有一个指向“更多结果”的链接。因此,进行这个查询并查看data['cursor']['estimatedResultCount']
来估计结果数量是相对快速的。
{'cursor': {'currentPageIndex': 0,
'estimatedResultCount': '168000000',
'moreResultsUrl': 'http://www.google.com/search?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=cars',
'pages': [{'label': 1, 'start': '0'},
{'label': 2, 'start': '4'},
{'label': 3, 'start': '8'},
{'label': 4, 'start': '12'},
{'label': 5, 'start': '16'},
{'label': 6, 'start': '20'},
{'label': 7, 'start': '24'},
{'label': 8, 'start': '28'}]},
'results': [ <<list of 4 dicts>> ]}