网站内容抓取(本文小编为如何在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 > 小发布日期。
让我们使用 BeautifulSoup 提取这些。
Python:
#!/usr/bin/python3
import requests
from bs4 import BeautifulSoup
from pprint import pprint
url = 'https://notes.ayushsharma.in/technology'
data = requests.get(url)
my_data = []
html = BeautifulSoup(data.text, 'html.parser')
articles = html.select('a.post-card')
for article in articles:
title = article.select('.card-title')[0].get_text()
excerpt = article.select('.card-text')[0].get_text()
pub_date = article.select('.card-footer small')[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:
[{'excerpt': "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.',
'pub_date': 'Aug 2021',
'title': 'Using variables in Jekyll to define custom content'},
{'excerpt': "In this article, I'll highlight some ideas for Jekyll "
'collections, blog category pages, responsive web-design, and '
'netlify.toml to make static website maintenance a breeze.',
'pub_date': 'Jul 2021',
'title': 'The evolution of ayushsharma.in: Jekyll, Bootstrap, Netlify, '
'static websites, and responsive design.'},
{'excerpt': "These are the top 5 lessons I've learned after 5 years of "
'Terraform-ing.',
'pub_date': 'Jul 2021',
'title': '5 key best practices for sane and usable Terraform setups'},
... (truncated)
看完这篇《Python中使用BeautifulSoup抓取网页内容的方法》文章介绍完毕。想要掌握这个文章的知识点,需要自己去实践和使用。了解,如果您想了解更多文章,请关注易素云行业资讯频道。