
- Python 網路爬蟲教程
- Python 網路爬蟲 - 首頁
- 簡介
- Python 入門
- Python 網路爬蟲模組
- 網路爬蟲的合法性
- 資料提取
- 資料處理
- 處理影像和影片
- 處理文字
- 爬取動態網站
- 爬取基於表單的網站
- 處理驗證碼
- 使用爬蟲進行測試
- Python 網路爬蟲資源
- Python 網路爬蟲 - 快速指南
- Python 網路爬蟲 - 資源
- Python 網路爬蟲 - 討論
Python 網路爬蟲 - 資料提取
分析網頁意味著理解其結構。現在,問題出現了,為什麼這對網路爬蟲很重要?在本章中,讓我們詳細瞭解一下。
網頁分析
網頁分析很重要,因為如果不進行分析,我們就無法知道在提取該網頁後,我們將以何種形式接收資料(結構化或非結構化)。我們可以透過以下方式進行網頁分析:
檢視頁面原始碼
這是一種透過檢查網頁的原始碼來了解其結構的方式。要實現這一點,我們需要右鍵單擊頁面,然後選擇“**檢視頁面原始碼**”選項。然後,我們將以 HTML 的形式從該網頁獲取我們感興趣的資料。但主要問題是關於空格和格式,這對我們來說很難格式化。
透過點選“檢查元素”選項檢查頁面原始碼
這是另一種分析網頁的方式。但不同之處在於它將解決網頁原始碼中格式和空格的問題。您可以透過右鍵單擊,然後從選單中選擇“**檢查**”或“**檢查元素**”選項來實現此操作。它將提供有關該網頁特定區域或元素的資訊。
從網頁提取資料的不同方法
以下方法主要用於從網頁提取資料:
正則表示式
它們是嵌入在 Python 中的高度專業化的程式語言。我們可以透過 Python 的**re** 模組來使用它。它也稱為 RE 或正則表示式或正則表示式模式。藉助正則表示式,我們可以為我們想要從資料中匹配的可能的字串集指定一些規則。
如果您想更全面地瞭解正則表示式,請訪問連結https://tutorialspoint.tw/automata_theory/regular_expressions.htm,如果您想了解有關 re 模組或 Python 中正則表示式的更多資訊,您可以訪問連結 https://tutorialspoint.tw/python/python_reg_expressions.htm。
示例
在以下示例中,我們將使用正則表示式匹配<td>的內容,從http://example.webscraping.com 中抓取關於印度的資料。
import re import urllib.request response = urllib.request.urlopen('http://example.webscraping.com/places/default/view/India-102') html = response.read() text = html.decode() re.findall('<td class="w2p_fw">(.*?)</td>',text)
輸出
相應的輸出將如下所示:
[ '<img src="/places/static/images/flags/in.png" />', '3,287,590 square kilometres', '1,173,108,018', 'IN', 'India', 'New Delhi', '<a href="/places/default/continent/AS">AS</a>', '.in', 'INR', 'Rupee', '91', '######', '^(\\d{6})$', 'enIN,hi,bn,te,mr,ta,ur,gu,kn,ml,or,pa,as,bh,sat,ks,ne,sd,kok,doi,mni,sit,sa,fr,lus,inc', '<div> <a href="/places/default/iso/CN">CN </a> <a href="/places/default/iso/NP">NP </a> <a href="/places/default/iso/MM">MM </a> <a href="/places/default/iso/BT">BT </a> <a href="/places/default/iso/PK">PK </a> <a href="/places/default/iso/BD">BD </a> </div>' ]
觀察到在上面的輸出中,您可以使用正則表示式看到關於印度國家的資訊。
Beautiful Soup
假設我們想從網頁中收集所有超連結,那麼我們可以使用一個名為 BeautifulSoup 的解析器,可以在https://www.crummy.com/software/BeautifulSoup/bs4/doc/.中詳細瞭解它。簡單來說,Beautiful Soup 是一個 Python 庫,用於從 HTML 和 XML 檔案中提取資料。它可以與 requests 一起使用,因為它需要一個輸入(文件或 URL)來建立 soup 物件,因為它本身無法獲取網頁。您可以使用以下 Python 指令碼收集網頁標題和超連結。
安裝 Beautiful Soup
使用**pip**命令,我們可以在我們的虛擬環境或全域性安裝中安裝**beautifulsoup**。
(base) D:\ProgramData>pip install bs4 Collecting bs4 Downloading https://files.pythonhosted.org/packages/10/ed/7e8b97591f6f456174139ec089c769f89 a94a1a4025fe967691de971f314/bs4-0.0.1.tar.gz Requirement already satisfied: beautifulsoup4 in d:\programdata\lib\sitepackages (from bs4) (4.6.0) Building wheels for collected packages: bs4 Running setup.py bdist_wheel for bs4 ... done Stored in directory: C:\Users\gaurav\AppData\Local\pip\Cache\wheels\a0\b0\b2\4f80b9456b87abedbc0bf2d 52235414c3467d8889be38dd472 Successfully built bs4 Installing collected packages: bs4 Successfully installed bs4-0.0.1
示例
請注意,在這個例子中,我們擴充套件了上面用 requests python 模組實現的例子。我們使用**r.text**來建立一個 soup 物件,該物件將進一步用於獲取網頁標題等詳細資訊。
首先,我們需要匯入必要的 Python 模組:
import requests from bs4 import BeautifulSoup
在下面的程式碼行中,我們使用 requests 對 URL:https://authoraditiagarwal.com/ 發出 GET HTTP 請求。
r = requests.get('https://authoraditiagarwal.com/')
現在我們需要建立一個 Soup 物件,如下所示:
soup = BeautifulSoup(r.text, 'lxml') print (soup.title) print (soup.title.text)
輸出
相應的輸出將如下所示:
<title>Learn and Grow with Aditi Agarwal</title> Learn and Grow with Aditi Agarwal
Lxml
我們將要討論的另一個用於網路爬蟲的 Python 庫是 lxml。它是一個高效能的 HTML 和 XML 解析庫。它相對快速且簡單。您可以在https://lxml.de/.上了解更多相關資訊。
安裝 lxml
使用 pip 命令,我們可以在我們的虛擬環境或全域性安裝中安裝**lxml**。
(base) D:\ProgramData>pip install lxml Collecting lxml Downloading https://files.pythonhosted.org/packages/b9/55/bcc78c70e8ba30f51b5495eb0e 3e949aa06e4a2de55b3de53dc9fa9653fa/lxml-4.2.5-cp36-cp36m-win_amd64.whl (3. 6MB) 100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 3.6MB 64kB/s Installing collected packages: lxml Successfully installed lxml-4.2.5
示例:使用 lxml 和 requests 進行資料提取
在以下示例中,我們使用 lxml 和 requests 從**authoraditiagarwal.com**抓取網頁的特定元素:
首先,我們需要匯入 requests 和來自 lxml 庫的 html,如下所示:
import requests from lxml import html
現在我們需要提供要抓取的網頁的 URL
url = https://authoraditiagarwal.com/leadershipmanagement/
現在我們需要提供該網頁特定元素的路徑**(Xpath)**:
path = '//*[@id="panel-836-0-0-1"]/div/div/p[1]' response = requests.get(url) byte_string = response.content source_code = html.fromstring(byte_string) tree = source_code.xpath(path) print(tree[0].text_content())
輸出
相應的輸出將如下所示:
The Sprint Burndown or the Iteration Burndown chart is a powerful tool to communicate daily progress to the stakeholders. It tracks the completion of work for a given sprint or an iteration. The horizontal axis represents the days within a Sprint. The vertical axis represents the hours remaining to complete the committed work.