使用 Python 代码执行 Google 搜索

2024 年 8 月 29 日 | 4 分钟阅读

Web Scraping(网页抓取)是一个抓取网页并从中提取详细信息的过程。与其为项目而定期从网页复制粘贴信息,不如使用网页抓取来有效地解决这个问题。但是,可用于网页抓取的网站很少。对于这个问题,有不同的解决方案。

  • 使用 Python 提供的 **selenium 和 beautifulSoup** 包。这些包可用于通过驱动程序抓取网页。
  • 另一个有效且简单的方法是一次点击同时找到 Google 搜索的所有链接。这可以使用 Python 提供的Google 包来实现。使用此包,我们将获得所有搜索结果的链接。

更多关于 Python 中的 Google 包

Python 提供了一个名为Google API 的 API,该 API 可以轻松准确地执行 Google 搜索。与 BeautifulSoup 一样,它也用于从 Google 抓取数据

在 Python 中安装 Google 包

由于它也依赖于 BeautifulSoup,因此我们需要在 Python 中安装 BeautifulSoup 包。

Google 包中使用的模块是googlesearch。googlesearch 模块中用于搜索网页并检索 URL 的函数是search()

Python 中 search 函数的语法是

Python 中 Google API 的函数和参数

  • lang:代表语言。
  • num:定义我们要搜索的结果数量。
  • query:是要搜索的字符串。
  • TLD:代表顶级域。它定义了我们要搜索结果的域名,例如 google.com、google.in 等。
  • start:定义要搜索的第一个结果。
  • stop:定义要搜索的最后一个结果。通过将 stop 值保留为 None,我们可以执行无限搜索。
  • return:定义一个迭代器,该迭代器以 URL 的形式提供搜索结果。如果 stop 参数保留为 None,它可以永远迭代。
  • pause:定义 HTTP 请求之间的等待间隔。Google 可能会在间隔很短的情况下阻止 IP 地址。

让我们通过在 Python 中实现它来理解 Google API。

程序 1:使用 Python 中的 Google API 进行简单搜索

代码

输出

https://tpointtech.cn/
https://www.youtube.com/channel/UCUnYvQVCrJoFWZhKK3O2xLg
https://in.linkedin.com/company/javatpoint
https://www.crunchbase.com/organization/javatpoint
https://6sense.com/company/javatpoint/5c3b0417d55ae49f1b7bd6af
https://javatpoint.academia.edu/JavaTpointcom
https://#/javatpoint/
https://www.instagram.com/javatpointofficial/?hl=en
https://github.com/javatpoint
https://#/training.javatpoint/
https://unogeeks.com/javatpoint-2/
https://twitter.com/pagejavatpoint?lang=en
https://www.apollo.io/companies/JavaTpoint-com/556e36377369641120dd5401
https://www.mouthshut.com/websites/Javatpoint-com-reviews-925747536
https://www.ambitionbox.com/overview/javatpoint-overview
https://issuu.com/javatpoint
https://www.geeksforgeeks.org/javatpoint-interview-experience/
https://www.quora.com/Is-studying-java-from-javatpoint-com-site-itself-enough-for-completing-core-Java
https://www.trustpilot.com/review/www.javatpoint.com
https://www.lusha.com/business/8f863326d3b3443c/

说明

首先,我们导入了 google 包。然后,我们将查询设置为javatpoint。使用search 函数,我们设置了不同参数的值来搜索查询。我们将tld设置为co.in搜索总数为 12stop 值为 20,即它将给出20 个 URL延迟时间为 2。

程序 2:使用 Python 搜索有关 Google 包的文章

代码

输出

https://www.simplilearn.com/tutorials/data-science-tutorial/what-is-data-science
https://www.geeksforgeeks.org/major-applications-of-data-science/
https://builtin.com/data-science/data-science-applications-examples
https://www.edureka.co/blog/data-science-applications/
https://online.hbs.edu/blog/post/what-is-data-science
https://tpointtech.cn/data-science
https://www.knowledgehut.com/blog/data-science/top-data-science-applications-for-future
https://intellipaat.com/blog/what-is-data-science/
https://www.coursera.org/articles/data-science-in-business
https://www.ibm.com/topics/data-science
https://www.scaler.com/topics/data-science/applications-of-data-science/
https://www.datacamp.com/blog/data-science-use-cases-guide
https://www.techtarget.com/searchbusinessanalytics/feature/8-top-data-science-applications-and-use-cases-for-businesses
https://www.investopedia.com/terms/d/data-science.asp
https://en.wikipedia.org/wiki/Data_science
https://data-flair.training/blogs/purpose-of-data-science/
https://aws.amazon.com/what-is/data-science/
https://data-flair.training/blogs/data-science-applications/
https://www.techtarget.com/searchenterpriseai/definition/data-science
https://www.mygreatlearning.com/blog/what-is-data-science/
https://hbr.org/2018/08/what-data-scientists-really-do-according-to-35-data-scientists
https://domino.ai/blog/10-interesting-uses-of-data-science
https://www.analyticssteps.com/blogs/10-data-science-applications-real-life
https://www.analyticsvidhya.com/blog/2015/09/applications-data-science/
https://w3schools.org.cn/datascience/ds_introduction.asp

说明

我们使用 Python 中的 google 模块搜索了文章“数据科学的用途”。我们将搜索总数设置为 14stop 值为 25,给出25 个结果。我们采用了4 秒的延迟,搜索时间更长。

程序 3:使用 Python 中的 Google API 进行无限搜索

代码

输出

https://www.simplilearn.com/what-skills-do-i-need-to-become-a-data-scientist-article
https://towardsdatascience.com/top-10-skills-for-a-data-scientist-in-2020-2b8e6122a742
https://www.coursera.org/articles/data-scientist-skills
https://www.geeksforgeeks.org/top-7-skills-required-to-become-a-data-scientist/
https://www.interviewbit.com/blog/data-scientist-skills/
https://www.tableau.com/learn/articles/data-science-skills
https://www.datacamp.com/blog/top-15-data-scientist-skills
https://www.springboard.com/blog/data-science/data-science-skills/
https://www.edureka.co/blog/data-scientist-skills/
https://www.analyticsvidhya.com/blog/2023/07/data-scientist-skills/
https://www.knowledgehut.com/blog/data-science/data-science-skills
https://www.projectpro.io/article/top-10-data-science-skills-to-master-in-2021/134
https://www.indeed.com/career-advice/resumes-cover-letters/skills-for-a-data-scientist
https://emeritus.org/in/learn/top-10-skills-for-a-successful-data-scientist/
https://www.kdnuggets.com/2023/04/top-19-skills-need-know-2023-data-scientist.html
https://www.techtarget.com/searchbusinessanalytics/feature/The-most-in-demand-data-science-skills-you-need
https://www.kdnuggets.com/2018/05/simplilearn-9-must-have-skills-data-scientist.html
https://www.scaler.com/topics/data-science/data-scientist-skills/
https://www.mygreatlearning.com/blog/data-scientist-skills/
https://intellipaat.com/blog/data-scientist-skills/
https://www.udacity.com/blog/2020/11/skills-to-launch-data-science-career.html
https://www.stratascratch.com/blog/what-skills-do-you-need-as-a-data-scientist/
https://365datascience.com/career-advice/career-guides/5-skills-data-science-job/
https://www.linkedin.com/pulse/top-skills-data-scientists-2022-michael-spencer-
https://www.usdsi.org/data-science-insights/data-science-skills-vs-tools-what-matters-the-most-for-data-scientists
https://bootcamp.berkeley.edu/blog/data-scientist-skills/
https://www.koenig-solutions.com/blog/data-scientist-skills
https://www.skillsyouneed.com/rhubarb/skills-data-scientist.html
https://medium.com/@springboard_ind/what-are-the-required-skills-for-data-science-cc8b6f8cd11 ....

说明

在此,我们使用 Python 中的 google 包搜索了主题“数据科学的基本技能”。我们将stop 值设置为 None。这意味着它将提供无限搜索。它会给出与查询相关的所有可能搜索。

我们可以通过在 Google 上自行搜索查询来检查结果。我们将获得与 Google API 输出相同的结果。

Python 是一门非常通用的语言,它为我们提供了可以直接从代码中搜索 Google 等搜索引擎的函数和模块。借助这些API 和库,可以实现自动化、抓取、数据提取等。