我试图使用xgoogle但我已经 3 年没有更新了,即使我设置每页 100 个结果,我也只能得到不超过 5 个结果。如果有人使用 xgoogle 没有任何问题,请告诉我。
现在,由于唯一可用的(显然)包装器是 xgoogle,因此可以选择使用某种浏览器,例如 mechanize,但这将使代码完全依赖于 google HTML,并且他们可能会对其进行很多更改。
最后一个选择是使用 google 提供的自定义搜索 API,但每天的请求数限制为 100 个,而且在此之后的定价也非常高。
我需要帮助,了解我应该朝哪个方向走,您还知道哪些其他选择以及什么对您有用。
谢谢!
最佳答案
它只需要一个小补丁。
函数 GoogleSearch._extract_result(search.py 的第 237 行)调用 GoogleSearch._extract_description(第 258 行),该函数失败,导致 _extract_result 对大多数结果返回 None,因此显示的结果少于预期。
修复:
在 search.py 中,将第 259 行更改为:
desc_div = result.find('div', {'class': re.compile(r'\bs\b')})
对此:
desc_div = result.find('span', {'class': 'st'})
我测试使用:
#!/usr/bin/python
#
# This program does a Google search for "quick and dirty" and returns
# 200 results.
#
from xgoogle.search import GoogleSearch, SearchError
class give_me(object):
def __init__(self, query, target):
self.gs = GoogleSearch(query)
self.gs.results_per_page = 50
self.current = 0
self.target = target
self.buf_list = []
def __iter__(self):
return self
def next(self):
if self.current >= self.target:
raise StopIteration
else:
if(not self.buf_list):
self.buf_list = self.gs.get_results()
self.current += 1
return self.buf_list.pop(0)
try:
sites = {}
for res in give_me("quick and dirty", 200):
t_dict = \
{
"title" : res.title.encode('utf8'),
"desc" : res.desc.encode('utf8'),
"url" : res.url.encode('utf8')
}
sites[t_dict["url"]] = t_dict
print t_dict
except SearchError, e:
print "Search failed: %s" % e
关于python - 使用 python 更新 Google 搜索方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17559709/