使用 Python 自动化 Google 搜索

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

一个完全自动化的搜索引擎 Google Scan 采用名为网络爬虫的软件持续搜索互联网,以将站点添加到我们的索引中。实际上,我们结果中显示的大部分页面并非手动添加;相反,它们是在我们的网络爬虫搜索互联网时自动发现和添加的。本文描述了搜索在您的网站上下文中如何运作的步骤。通过掌握这些基本信息,您可以解决爬取问题,让您的页面被索引,并学习如何改进您的网站在 Google 搜索中的显示方式。

Google 搜索,通常称为 Google,是 Google 提供的搜索引擎。它在全球搜索引擎市场中占据 92% 的份额,每天处理超过 35 亿次搜索。此外,它在全球范围内获得最多的访问量。Google 使用一种名为“PageRank”的优先级排名机制来确定它返回的搜索结果的顺序。此外,Google 搜索提供了广泛的专门交互式体验选项,包括航班状态和包裹跟踪、天气预报、货币、单位和时间转换、单词定义等等。符号可以包含、排除、指定或要求某些搜索行为。

与其他类型的数据(例如照片或数据库中找到的信息)不同,Google 搜索的主要功能是搜索 Web 服务器提供的公开文档中的文本。它最初由 Scott Hassan、Larry Page 和 Sergey Brin 于 1997 年创建。Google 于 2011 年 6 月宣布了“Google 语音搜索”,允许用户搜索口语而不是书面语。Google 于 2012 年 5 月在美国推出了知识图谱语义搜索功能。

通过分析搜索词的频率,可以显示经济、社会和健康趋势。Google Trends 允许公开查询有关搜索词使用频率的数据,这些数据已被发现与流感流行和失业率相关。它还比传统的报告技术和调查更快地提供信息。Google 的搜索引擎在 2016 年年中开始使用深度神经网络。

并非所有页面都通过 Google 搜索三阶段过程的每个级别

  • 爬取:Google 使用称为爬虫的自动化工具从它在网上找到的网页中检索文本、图像和视频。
  • 索引:Google 扫描页面的文本、图像和视频文件,然后将其内容存储在其庞大的数据库 Google 索引中。
  • 提供搜索结果:当一个人在 Google 上进行搜索时,Google 会返回与其查询相关的结果。

爬取

识别网络上现有页面是第一步。Google 必须不断搜索新的和更新的网页,以将其添加到其已知页面数据库中,因为所有在线页面都没有一个存储库。“URL 发现”是此过程的名称。由于 Google 之前已查看过某些页面,因此它们是众所周知的。当 Google 从已知网站跟随链接到新页面时,会发现更多页面。例如,中心页面(例如类别页面)可以链接到新的博客文章。当您向 Google 提供要爬取的页面列表(站点地图)时,会发现更多页面。

Google 在了解其 URL 后可能会访问(或“爬取”)页面,以查看其中有什么。我们使用大量的计算机阵列抓取数十亿个网页。执行检索的软件称为 Googlebot(也称为机器人、bot 或蜘蛛)。Googlebot 使用算法方法来选择要爬取的网站、频率以及从每个网站抓取多少页面。为了防止网站过载,Google 的爬虫也配置为注意不要爬取过快。此方法基于网站的响应(例如,HTTP 500 故障表示“减速”)和 Search Console 设置。

然而,Googlebot 并非抓取它找到的每个网站。某些页面可能未被网站所有者抓取,而其他页面可能需要登录才能访问,还有一些页面可能是已抓取网站的副本。例如,尽管 www.example.com 和 example.com 具有相同的内容,但许多网站可以通过域名的任何变体进行访问。

类似于您的浏览器渲染您查看的网站的方式,Google 在抓取过程中渲染页面并执行它发现的任何 JavaScript。渲染至关重要,因为如果没有它,Google 将无法看到网站经常使用 JavaScript 传递到页面的内容。

爬取取决于 Google 的爬虫是否能够访问该网站。Googlebot 访问网站的常见问题包括

  • 处理站点的服务器存在问题。
  • 网络问题
  • robots.txt 指令限制 Googlebot 访问网站

索引

Google 在抓取页面后尝试确定页面的主题。在此步骤中,称为索引,将处理和检查文本内容以及重要标签和属性,包括标题组件和 alt 属性,以及图像、视频和其他类型的内容。

Google 在索引过程中决定页面是另一个互联网页面的副本还是规范页面。可能出现在搜索结果中的页面称为规范页面。我们收集了我们在网上发现的具有相似信息的网站,然后选择最能代表该组的网站作为规范页面。该组中其他页面的替代版本可以在许多设置中使用,包括。

在索引过程中,Google 会评估页面是规范页面还是另一个互联网页面的副本。可能出现在搜索结果中的页面是规范页面。为了选择规范页面,我们首先将我们发现的具有相似内容的网站分组,然后选择最能代表该组的网站。该组中的其他页面是替代版本,可以根据情况显示,例如用户在移动设备上搜索该集群中的特定页面。

在后续步骤中,当我们在搜索结果中提供页面时,Google 还会收集有关规范页面及其内容的信号。页面的语言、内容来源国家/地区、页面的可用性以及其他因素都是信号的示例。Google 索引是一个庞大的数据库,存储在数百台计算机上,可能包含有关规范页面及其集群的收集数据。并非 Google 处理的每个页面都会被索引,因此索引不能保证。页面的元数据和内容也会影响索引。典型的索引问题包括以下内容

  • 页面内容质量差。
  • 元机器人指令禁止索引
  • 网站布局可能使索引变得困难。

提供搜索结果

当用户进行查询时,我们的计算机在索引中查找匹配的页面,并返回我们认为质量最高且与用户最相关的结果。许多变量,包括用户的位置、语言和设备,都用于评估相关性(桌面或手机)。例如,在巴黎的用户和在香港的用户在搜索“自行车修理店”时可能会看到不同的结果。

即使 Search Console 可能会声称页面已被索引,它也不会出现在搜索结果中。这可能是由于

  • 用户不会关心页面上的内容。
  • 内容质量差。
  • Robots 的元指令阻止它们提供服务

最全面的搜索类型是自然搜索,它几乎涵盖了所有人。任何拥有网站并希望其内容或品牌在网上被发现的公司、团体或个人都应努力在自然搜索结果中显示。

最全面的搜索类型,包括几乎所有人,是自然搜索。对于任何运营网站并希望其内容或品牌在网上被看到的组织、团体或个人来说,争取在自然搜索结果中显示是一个很好的策略。营利性公司通常利用广告来接触目标市场,获取更多潜在客户并实现销售。广告可以被本地、全国和全球组织使用,因为它们不仅可以根据关键词定位人群,还可以根据地理位置定位人群。Google Ad Grants 是一项计划,为 501(c)(3) 组织提供捐赠的搜索广告以进行社区参与,使非政府组织能够将 Google Ads 用于基于事业的活动。

代码

# 这是一个示例 Python 程序,用于自动化 Google 搜索过程,借助此程序,用户可以轻松地使用 Google 搜索引擎搜索互联网,并提供用户想要获取数据的特定搜索关键字。我们有不同的功能来满足此特定实用程序的不同功能

# 执行此填充所需的所有库都在此程序开头导入

from googlesearch import search

# 编写了一个类,该类具有不同的函数来执行成功 Google 搜索所需的国家/地区。该类内部有不同的函数,例如用于初始化此程序所需的不同类对象的构造函数,然后我们有一个函数应该获取用户希望为特定主题看到的搜索结果数量,因此用户可以决定他希望为他正在搜索的特定关键字看到的搜索数量,然后我们有函数以关键字作为输入,实际的 Google 搜索将执行,然后有一个函数来执行实际的 Google 搜索,并且该搜索的结果存储到列表中,当用户保存最后一个函数(即打印他输入的关键字的所有 Google 搜索结果)时,该列表将被创建

# 这是一个构造函数,用于初始化在此类内部编写的各种函数中使用的各种变量或我们可以说类变量。在构造函数中,所有这些变量都初始化为 None 值,然后稍后在代码中初始化为实际值

# 此函数用于从用户获取输入,以定义用户希望为特定 Google 搜索显示的搜索数量,此函数的输入是整数类型,除以输入后将为每个 Google 搜索结果向用户显示搜索结果数量。一旦用户设置,所有 Google 搜索都将显示此数量的搜索结果,但用户可以随时通过再次调用此函数并提供不同的值来更改要显示的搜索结果数量。

# 此函数用于从用户获取关键字输入,实际的搜索将在调用此函数时执行,用户将看到提示文本“输入 Google 搜索的文本”,之后用户应输入一个有效的字符串,该字符串存储在关键字搜索变量中,然后稍后用于执行实际的 Google 搜索

# 这是执行实际 Google 搜索填充的实际函数,在此函数中,在上面编写的函数中作为输入获取的关键字用于执行 Google 搜索,并且除了该关键字之外,我们还使用我们需要显示的搜索计数,我们已经在前面的函数中获取了输入。根据用户提供的这两个输入,我们执行 Google 搜索,并且从该 Google 搜索获得的结果存储到结果字典中,然后使用在此特定函数之后编写的另一个函数打印该字典。在此函数中,实际 Google 搜索的操作在 try except 块中执行,这确保了在 Google 搜索过程中为用户提供的键盘输入发生任何错误异常时,该异常将在该函数本身中处理,并且会向用户显示一条消息,说明 Google 搜索遇到了异常,并且还会向用户显示执行 Google 搜索时遇到的特定异常或错误的描述,这将有助于更好地理解问题。

# 此函数用于打印在前面函数中获得的搜索结果,此函数打印存储在结果列表中的所有搜索,该列表在前一步中填充,并且该列表中的所有条目都与搜索编号一起打印,该列表中的条目数量将等于用户通过指定用户希望为特定 Google 搜索看到的搜索结果数量所提供的值。

# 这是主函数,在此主函数内部创建了上述编写过程的对象,借助此类的对象,我们可以调用该类内部编写的所有各种函数。在此主函数中,我们编写了一个 while 块,该块将不断为用户打印菜单选项,并且菜单中列出了 20 个选项,例如设置用户需要为每个 Google 搜索显示的搜索数量,输入要执行实际搜索操作的关键字,为用户在上一步中输入的关键字执行 Google 搜索并将该 Google 搜索的结果存储到结果列表中,以及打印在上一步中获得结果的 Google 搜索结果,最后一个选项是退出代码执行。根据用户输入的选项,调用该特定函数,如果该函数需要用户的一些输入,则会向用户提示实际消息,要求用户向该函数提供有效输入,以便执行特定操作,如果用户不需要任何输入,则调用该函数并执行该操作,如果发生任何异常或错误,用户还会打印该特定错误或异常消息以及错误消息,菜单将一直打印,直到用户选择退出代码执行。

输出

Select any one of the valid operations which are listed below:
1. To set the number of searches we need to display for each Google Search.
2. To enter the keyword for the Google Search.
3. To perform Google Search for the keyword entered by the user.
4. To print the Google search results obtained after searching.
5. To exit from the code execution.
1
Enter the number of search results you want to display for each Google search.
10
To go on with the code getting executed, enter input [y] or [n]
y
Select any one of the valid operations which are listed below:
1. To set the number of searches we need to display for each Google Search.
2. To enter the keyword for the Google Search.
3. To perform Google Search for the keyword entered by the user.
4. To print the Google search results obtained after searching.
5. To exit from the code execution.
2
Enter the text for Google Search::
python
To go on with the code getting executed, enter input [y] or [n]
y
Select any one of the valid operations which are listed below:
1. To set the number of searches we need to display for each Google Search.
2. To enter the keyword for the Google Search.
3. To perform Google Search for the keyword entered by the user.
4. To print the Google search results obtained after searching.
5. To exit from the code execution.
3
Google Search performed successfully.
To go on with the code getting executed, enter input [y] or [n]
y
Select any one of the valid operations which are listed below:
1. To set the number of searches we need to display for each Google Search.
2. To enter the keyword for the Google Search.
3. To perform Google Search for the keyword entered by the user.
4. To print the Google search results obtained after searching.
5. To exit from the code execution.
4
The Results for python keyword are ::
Search No . 1 -> https://pythonlang.cn/
Search No . 2 -> https://pythonlang.cn/
Search No . 3 -> https://w3schools.org.cn/python/
Search No . 4 -> https://en.wikipedia.org/wiki/Python_(programming_language)
Search No . 5 -> https://programiz.com.cn/python-programming/online-compiler/
Search No . 6 -> https://www.codecademy.com/catalog/language/python
Search No . 7 -> https://tutorialspoint.org.cn/python/index.htm
Search No . 8 -> https://www.learnpython.org/
Search No . 9 -> https://www.coursera.org/specializations/python
Search No . 10 -> https://www.coursera.org/specializations/python
To go on with the code getting executed, enter input [y] or [n]
y
Select any one of the valid operations which are listed below:
1. To set the number of searches we need to display for each Google Search.
2. To enter the keyword for the Google Search.
3. To perform Google Search for the keyword entered by the user.
4. To print the Google search results obtained after searching.
5. To exit from the code execution.
2
Enter the text for Google Search::
linux
To go on with the code getting executed, enter input [y] or [n]
y
Select any one of the valid operations which are listed below:
1. To set the number of searches we need to display for each Google Search.
2. To enter the keyword for the Google Search.
3. To perform Google Search for the keyword entered by the user.
4. To print the Google search results obtained after searching.
5. To exit from the code execution.
3
Google Search performed successfully.
To go on with the code getting executed, enter input [y] or [n]
y
Select any one of the valid operations which are listed below:
1. To set the number of searches we need to display for each Google Search.
2. To enter the keyword for the Google Search.
3. To perform Google Search for the keyword entered by the user.
4. To print the Google search results obtained after searching.
5. To exit from the code execution.
4
The Results for linux keyword are ::
Search No . 1 -> https://www.linux.org/
Search No . 2 -> https://en.wikipedia.org/wiki/Linux
Search No . 3 -> https://www.linuxfoundation.org/
Search No . 4 -> https://www.linux.com/what-is-linux/
Search No . 5 -> https://ubuntu.com/
Search No . 6 -> https://www.techtarget.com/searchdatacenter/definition/Linux-operating-system
Search No . 7 -> https://linuxmint.com/
Search No . 8 -> https://open-source.net.cn/resources/linux
Search No . 9 -> https://linuxkernel.org.cn/
Search No . 10 -> https://www.oracle.com/in/linux/
To go on with the code getting executed, enter input [y] or [n]
y
Select any one of the valid operations which are listed below:
1. To set the number of searches we need to display for each Google Search.
2. To enter the keyword for the Google Search.
3. To perform Google Search for the keyword entered by the user.
4. To print the Google search results obtained after searching.
5. To exit from the code execution.
1
Enter the number of search results you want to display for each Google search.
6
To go on with the code getting executed, enter input [y] or [n]
y
Select any one of the valid operations which are listed below:
1. To set the number of searches we need to display for each Google Search.
2. To enter the keyword for the Google Search.
3. To perform Google Search for the keyword entered by the user.
4. To print the Google search results obtained after searching.
5. To exit from the code execution.
2
Enter the text for Google Search::
microcomputer
To go on with the code getting executed, enter input [y] or [n]
y
Select any one of the valid operations which are listed below:
1. To set the number of searches we need to display for each Google Search.
2. To enter the keyword for the Google Search.
3. To perform Google Search for the keyword entered by the user.
4. To print the Google search results obtained after searching.
5. To exit from the code execution.
3
Google Search performed successfully.
To go on with the code getting executed, enter input [y] or [n]
y
Select any one of the valid operations which are listed below:
1. To set the number of searches we need to display for each Google Search.
2. To enter the keyword for the Google Search.
3. To perform Google Search for the keyword entered by the user.
4. To print the Google search results obtained after searching.
5. To exit from the code execution.
4
The Results for microcomputer keyword are ::
Search No . 1 -> https://www.britannica.com/technology/microcomputer
Search No . 2 -> https://ecomputernotes.com/fundamental/introduction-to-computer/microcomputer
Search No . 3 -> https://www.techopedia.com/definition/4614/microcomputer
Search No . 4 -> https://www.sciencedirect.com/topics/earth-and-planetary-sciences/microcomputer
Search No . 5 -> https://www.techopedia.com/definition/4614/microcomputer
Search No . 6 -> https://www.khanacademy.org/computing/computers-and-internet/xcae6f4a7ff015e7d:computers/xcae6f4a7ff015e7d:computer-components/a/exploring-microcomputers

因此,在上面编写的代码中,在此主函数内部,创建了上面编写的过程的对象,借助此类的对象,我们可以调用该类内部编写的所有各种函数。在此主函数中,我们编写了一个 while 块,该块将不断为用户打印菜单选项,并且菜单中列出了 20 个选项,例如设置用户需要为每个 Google 搜索显示的搜索数量,输入要执行实际搜索操作的关键字,为用户在上一步中输入的关键字执行 Google 搜索并将该 Google 搜索的结果存储到结果列表中,以及打印在上一步中获得结果的 Google 搜索结果,最后一个选项是退出代码执行。根据用户输入的选项,调用该特定函数,如果该函数需要用户的一些输入,则会向用户提示实际消息,要求用户向该函数提供有效输入,以便执行特定操作,如果用户不需要任何输入,则调用该函数并执行该操作,如果发生任何异常或错误,用户还会打印该特定错误或异常消息以及错误消息,菜单将一直打印,直到用户选择退出代码执行。

因此,在本文中,我们了解了如何使用 Python 自动化 Google 搜索。