如何通过 python 包查找 Google 搜索结果

How to find Google Search results via a python package

我目前正在尝试通过 Python 获取 Google 个搜索结果列表。

几年前 google 更改了 html 布局时,许多不同的包已停止工作或已被弃用,例如pygoogle, xgoogle

在 Google 上搜索 "Hiking Trails Los Angeles",如何 return 前 10 个结果,最好带有 url、标题和描述或其他可用属性?

Yh,google-search-api has been deprecated, hence the pygoogle 是 Google 搜索 api 的包装器。在搜索 api 页面的顶部,有一个警告,以及:

We encourage you to investigate the Custom Search API, which may provide an alternative solution.


但是使用这个自定义搜索 api 来搜索整个网络并不是很简单。我在这里找到了 2 个详细指南(SO 答案):

  • 1st step: get Google API key.
  • 2nd step: setup Custom Search Engine so that you can search the entire web.
  • 3rd step: install Google API client for Python.
  • 4th step (bonus): do the search.

So, after setting this up, you can follow the code samples from few places:


  1. What are the alternatives now that the Google web search API has been deprecated?

Yes, Google Custom Search has now replaced the old Search API, but you can still use Google Custom Search to search the entire web, although the steps are not obvious from the Custom Search setup.

To create a Google Custom Search engine that searches the entire web:

  1. From the Google Custom Search homepage ( http://www.google.com/cse/ ), click Create a Custom Search Engine.
  2. Type a name and description for your search engine.
  3. Under Define your search engine, in the Sites to Search box, enter at least one valid URL (For now, just put www.anyurl.com to get past this screen. More on this later ).
  4. Select the CSE edition you want and accept the Terms of Service, then click Next. Select the layout option you want, and then click Next.
  5. Click any of the links under the Next steps section to navigate to your Control panel.
  6. In the left-hand menu, under Control Panel, click Basics.
  7. In the Search Preferences section, select Search the entire web but emphasize included sites.
  8. Click Save Changes.
  9. In the left-hand menu, under Control Panel, click Sites.
  10. Delete the site you entered during the initial setup process.

Google 自定义搜索并不是一直免费的,即 Pricing:

  • Custom Search Engine (free) For CSE users, the API provides 100 search queries per day for free. If you need more, you may sign up for billing in the API Console. Additional requests cost per 1000 queries, up to 10k queries per day.
  • Google Site Search (paid). For detailed information on GSS usage limits and quotas, please check GSS pricing options.