自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python Beautiful Soup 刮取簡易指南

開發(fā) 后端
今天我們將討論如何使用 Beautiful Soup 庫從 HTML 頁面中提取內(nèi)容,之后,我們將使用它將其轉(zhuǎn)換為 Python 列表或字典。

[[440826]]

Python 中的 Beautiful Soup 庫可以很方便的從網(wǎng)頁中提取 HTML 內(nèi)容。

今天我們將討論如何使用 Beautiful Soup 庫從 HTML 頁面中提取內(nèi)容,之后,我們將使用它將其轉(zhuǎn)換為 Python 列表或字典。

什么是 Web 刮取,為什么我需要它?

答案很簡單:并非每個網(wǎng)站都有獲取內(nèi)容的 API。你可能想從你最喜歡的烹飪網(wǎng)站上獲取食譜,或者從旅游博客上獲取照片。如果沒有 API,提取 HTML(或者說 刮取scraping 可能是獲取內(nèi)容的唯一方法。我將向你展示如何使用 Python 來獲取。

并非所以網(wǎng)站都喜歡被刮取,有些網(wǎng)站可能會明確禁止。請于網(wǎng)站所有者確認是否同意刮取。

Python 如何刮取網(wǎng)站?

使用 Python 進行刮取,我們將執(zhí)行三個基本步驟:

  1. 使用 requests 庫獲取 HTML 內(nèi)容
  2. 分析 HTML 結(jié)構(gòu)并識別包含我們需要內(nèi)容的標簽
  3. 使用 Beautiful Soup 提取標簽并將數(shù)據(jù)放入 Python 列表中

安裝庫

首先安裝我們需要的庫。requests 庫從網(wǎng)站獲取 HTML 內(nèi)容,Beautiful Soup 解析 HTML 并將其轉(zhuǎn)換為 Python 對象。在 Python3 中安裝它們,運行:

  1. pip3 install requests beautifulsoup4

提取 HTML

在本例中,我將選擇刮取網(wǎng)站的 Techhology 部分。如果你跳轉(zhuǎn)到此頁面,你會看到帶有標題、摘錄和發(fā)布日期的文章列表。我們的目標是創(chuàng)建一個包含這些信息的文章列表。

網(wǎng)站頁面的完整 URL 是:

  1. https://notes.ayushsharma.in/technology

我們可以使用 requests 從這個頁面獲取 HTML 內(nèi)容:

  1. #!/usr/bin/python3
  2. import requests
  3.  
  4. url = 'https://notes.ayushsharma.in/technology'
  5.  
  6. data = requests.get(url)
  7.  
  8. print(data.text)

變量 data 將包含頁面的 HTML 源代碼。

從 HTML 中提取內(nèi)容

為了從 data 中提取數(shù)據(jù),我們需要確定哪些標簽具有我們需要的內(nèi)容。

如果你瀏覽 HTML,你會發(fā)現(xiàn)靠近頂部的這一段:

  1. <div class="col">
  2. <a href="/2021/08/using-variables-in-jekyll-to-define-custom-content" class="post-card">
  3. <div class="card">
  4. <div class="card-body">
  5. <h5 class="card-title">Using variables in Jekyll to define custom content</h5>
  6. <small class="card-text text-muted">I recently discovered that Jekyll's config.yml can be used to define custom
  7. variables for reusing content. I feel like I've been living under a rock all this time. But to err over and
  8. over again is human.</small>
  9. </div>
  10. <div class="card-footer text-end">
  11. <small class="text-muted">Aug 2021</small>
  12. </div>
  13. </div>
  14. </a>
  15. </div>

這是每篇文章在整個頁面中重復(fù)的部分。我們可以看到 .card-title 包含文章標題,.card-text 包含摘錄,.card-footer > small 包含發(fā)布日期。

讓我們使用 Beautiful Soup 提取這些內(nèi)容。

  1. #!/usr/bin/python3
  2. import requests
  3. from bs4 import BeautifulSoup
  4. from pprint import pprint
  5.  
  6. url = 'https://notes.ayushsharma.in/technology'
  7. data = requests.get(url)
  8.  
  9. my_data = []
  10.  
  11. html = BeautifulSoup(data.text, 'html.parser')
  12. articles = html.select('a.post-card')
  13.  
  14. for article in articles:
  15.  
  16. title = article.select('.card-title')[0].get_text()
  17. excerpt = article.select('.card-text')[0].get_text()
  18. pub_date = article.select('.card-footer small')[0].get_text()
  19.  
  20. my_data.append({"title": title, "excerpt": excerpt, "pub_date": pub_date})
  21.  
  22. pprint(my_data)

以上代碼提取文章信息并將它們放入 my_data 變量中。我使用了 pprint 來美化輸出,但你可以在代碼中忽略它。將上面的代碼保存在一個名為 fetch.py 的文件中,然后運行它:

  1. python3 fetch.py

如果一切順利,你應(yīng)該會看到:

  1. [{'excerpt': "I recently discovered that Jekyll's config.yml can be used to"
  2. "define custom variables for reusing content. I feel like I've"
  3. 'been living under a rock all this time. But to err over and over'
  4. 'again is human.',
  5. 'pub_date': 'Aug 2021',
  6. 'title': 'Using variables in Jekyll to define custom content'},
  7. {'excerpt': "In this article, I'll highlight some ideas for Jekyll"
  8. 'collections, blog category pages, responsive web-design, and'
  9. 'netlify.toml to make static website maintenance a breeze.',
  10. 'pub_date': 'Jul 2021',
  11. 'title': 'The evolution of ayushsharma.in: Jekyll, Bootstrap, Netlify,'
  12. 'static websites, and responsive design.'},
  13. {'excerpt': "These are the top 5 lessons I've learned after 5 years of"
  14. 'Terraform-ing.',
  15. 'pub_date': 'Jul 2021',
  16. 'title': '5 key best practices for sane and usable Terraform setups'},
  17.  
  18. ... (truncated)

以上是全部內(nèi)容!在這 22 行代碼中,我們用 Python 構(gòu)建了一個網(wǎng)絡(luò)刮取器,你可以在 我的示例倉庫中找到源代碼。

總結(jié)

對于 Python 列表中的網(wǎng)站內(nèi)容,我們現(xiàn)在可以用它做一些很酷的事情。我們可以將它作為 JSON 返回給另一個應(yīng)用程序,或者使用自定義樣式將其轉(zhuǎn)換為 HTML。隨意復(fù)制粘貼以上代碼并在你最喜歡的網(wǎng)站上進行試驗。

玩的開心,繼續(xù)編碼吧。 

責(zé)任編輯:龐桂玉 來源: Linux中國
相關(guān)推薦

2023-11-28 08:34:39

Python工具

2023-12-08 18:05:12

文本爬蟲Python

2022-06-28 13:41:07

瀏覽網(wǎng)頁互聯(lián)網(wǎng)交互Python

2023-01-04 17:59:00

BeautifulPython

2020-08-17 07:00:00

數(shù)據(jù)遷移數(shù)據(jù)中心技術(shù)

2011-10-27 11:32:36

Google云計算SQL數(shù)據(jù)庫

2014-05-27 10:40:07

CentOS

2025-02-12 08:00:00

AI機器學(xué)習(xí)學(xué)習(xí)模型

2021-10-19 22:23:47

CSSBeautiful按鈕

2015-11-11 15:19:13

Linux編譯調(diào)試

2025-02-03 06:00:00

2022-02-21 07:02:16

CSSbeautiful按鈕

2024-08-05 09:58:24

2011-04-18 10:52:17

Jpcap

2019-05-07 08:58:53

Python代碼Web

2017-10-26 13:40:11

Python一行代碼

2021-11-11 12:05:17

Python代碼項目

2014-07-28 09:52:14

PythonPython性能

2024-03-19 07:54:57

FunctoolsPython函數(shù)式編程

2011-01-11 15:51:35

Pythonwebipad
點贊
收藏

51CTO技術(shù)棧公眾號