网站内容抓取(本文小编为如何在Python中执行此操作?“)

优采云 发布时间: 2022-01-31 17:04

  网站内容抓取(本文小编为如何在Python中执行此操作?“)

  在这篇文章中,小编将详细介绍“Python中使用BeautifulSoup抓取网页内容的方法”。内容详细,步骤清晰,细节处理得当。希望这篇《Python中使用BeautifulSoup抓取网页内容的方法》文章可以帮助大家解决疑惑,跟着小编的思路慢慢深入,一起学习新知识。

  什么是网页抓取?

  简短的回答是:并非每个 网站 都有用于获取内容的 API。您可能想从您最喜欢的厨师那里获取食谱网站 或从旅游博客中获取照片。如果没有 API,提取 HTML 或抓取可能是获取该内容的唯一方法。我将向您展示如何在 Python 中执行此操作。

  注意:并不是所有的网站都喜欢刮,有的网站可能会明示禁止。请与 网站 的所有者确认可抓取性。

  如何在 Python 中抓取 网站?

  为了使网络抓取在 Python 中工作,我们将执行 3 个基本步骤:

  使用 requests 库提取 HTML 内容。

  分析 HTML 结构并识别收录我们内容的标签。

  使用 BeautifulSoup 提取标签并将数据放入 Python 列表中。

  安装库

  让我们首先安装我们需要的库。请求从 网站 获取 HTML 内容。BeautifulSoup 解析 HTML 并将其转换为 Python 对象。要为 Python 3 安装这些,请运行:

  pip3 install requests beautifulsoup4

  提取 HTML

  在这个例子中,我将选择抓取 网站 的技术部分。如果您访问此页面,您将看到一个 文章 列表,其中收录标题、摘录和发布日期。我们的目标是创建一个收录该信息的 文章 列表。

  技术页面的完整 URL 是:

  https://notes.ayushsharma.in/technology

  我们可以使用 Requests 从此页面获取 HTML 内容:

  #!/usr/bin/python3

import requests

url = 'https://notes.ayushsharma.in/technology'

data = requests.get(url)

print(data.text)

  变量数据将收录页面的 HTML 源代码。

  从 HTML 中提取内容

  为了从收到的 HTML 中提取我们的数据,我们需要确定哪些标签具有我们需要的内容。

  如果您浏览 HTML,您会在顶部附近找到此部分:

  HTML:

  

  

    

      

        Using variables in Jekyll to define custom content

        I recently discovered that Jekyll's config.yml can be used to define custom

          variables for reusing content. I feel like I've been living under a rock all this time. But to err over and

          over again is human.

      

      

        Aug 2021

      

    

  </a>

  这是贯穿每个 文章 页面的重复部分。我们可以看到 .card-title 有 文章 标题、.card-text 摘录和 .card-footer &gt; 小发布日期。

  让我们使用 BeautifulSoup 提取这些。

  Python:

  #!/usr/bin/python3

import requests

from bs4 import BeautifulSoup

from pprint import pprint

url = &#39;https://notes.ayushsharma.in/technology&#39;

data = requests.get(url)

my_data = []

html = BeautifulSoup(data.text, &#39;html.parser&#39;)

articles = html.select(&#39;a.post-card&#39;)

for article in articles:

    title = article.select(&#39;.card-title&#39;)[0].get_text()

    excerpt = article.select(&#39;.card-text&#39;)[0].get_text()

    pub_date = article.select(&#39;.card-footer small&#39;)[0].get_text()

    my_data.append({"title": title, "excerpt": excerpt, "pub_date": pub_date})

pprint(my_data)

  上面的代码将提取 文章 并将它们放入 my_data 变量中。我正在使用 pprint 来漂亮地打印输出,但您可以在自己的代码中跳过它。将上述代码保存在一个名为 fetch.py​​ 的文件中并运行它:

  python3 fetch.py

  如果一切顺利,您应该看到:

  Python:

  [{&#39;excerpt&#39;: "I recently discovered that Jekyll&#39;s config.yml can be used to "

"define custom variables for reusing content. I feel like I&#39;ve "

&#39;been living under a rock all this time. But to err over and over &#39;

&#39;again is human.&#39;,

&#39;pub_date&#39;: &#39;Aug 2021&#39;,

&#39;title&#39;: &#39;Using variables in Jekyll to define custom content&#39;},

{&#39;excerpt&#39;: "In this article, I&#39;ll highlight some ideas for Jekyll "

&#39;collections, blog category pages, responsive web-design, and &#39;

&#39;netlify.toml to make static website maintenance a breeze.&#39;,

&#39;pub_date&#39;: &#39;Jul 2021&#39;,

&#39;title&#39;: &#39;The evolution of ayushsharma.in: Jekyll, Bootstrap, Netlify, &#39;

&#39;static websites, and responsive design.&#39;},

{&#39;excerpt&#39;: "These are the top 5 lessons I&#39;ve learned after 5 years of "

&#39;Terraform-ing.&#39;,

&#39;pub_date&#39;: &#39;Jul 2021&#39;,

&#39;title&#39;: &#39;5 key best practices for sane and usable Terraform setups&#39;},

... (truncated)

  看完这篇《Python中使用BeautifulSoup抓取网页内容的方法》文章介绍完毕。想要掌握这个文章的知识点,需要自己去实践和使用。了解,如果您想了解更多文章,请关注易素云行业资讯频道。

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线