Extract url from text: Difference between revisions
m (→擷取特定檔案類型的網址) |
|||
| Line 48: | Line 48: | ||
# 網址可能是 <nowiki>http://</nowiki> 或 <nowiki>https://</nowiki> 開頭,所以條件是 {{kbd | key = <nowiki>http[s]?://</nowiki>}} | # 網址可能是 <nowiki>http://</nowiki> 或 <nowiki>https://</nowiki> 開頭,所以條件是 {{kbd | key = <nowiki>http[s]?://</nowiki>}} | ||
# 根據 [http://tools.ietf.org/html/rfc3986/ RFC 3986] 的 [http://tools.ietf.org/html/rfc3986#section-2 Section 2: Characters] 網址允許的文字有 {{kbd | key = <nowiki>ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=</nowiki>}},其他文字則需要加上比例符號 % 編碼。 <ref>[http://stackoverflow.com/questions/1547899/which-characters-make-a-url-invalid validation - Which characters make a URL invalid? - Stack Overflow]</ref> | # 根據 [http://tools.ietf.org/html/rfc3986/ RFC 3986] 的 [http://tools.ietf.org/html/rfc3986#section-2 Section 2: Characters] 網址允許的文字有 {{kbd | key = <nowiki>ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=</nowiki>}},其他文字則需要加上比例符號 % 編碼。 <ref>[http://stackoverflow.com/questions/1547899/which-characters-make-a-url-invalid validation - Which characters make a URL invalid? - Stack Overflow]</ref> | ||
== 從 HTML 文字,擷取完整網址 == | |||
=== 使用 Google sheet 擷取完整網址 === | |||
# Using [https://extract-urls.contributor.pw/ EXTRACT URLs] to extracts links and converts them to the HYPERLINK formula. | |||
# Using [https://support.google.com/docs/answer/9365792?hl=en FORMULATEXT function - Google Docs Editors Help] | |||
# Using [https://support.google.com/docs/answer/3098244?hl=zh-Hant REGEXEXTRACT] function to extract the Url from above cell | |||
<pre> | |||
=REGEXEXTRACT(A1, "(http[s]?://[a-zA-Z0-9\-_\\._~\:\/\?#\[\]@\!\$&'\(\)\*\+,;\=%]+)") | |||
</pre> | |||
參考資料: | |||
* [https://support.google.com/docs/thread/34116680/extract-url-from-pasted-external-text-with-link-embedded?hl=en Extract URL from pasted external text with link embedded - Google Docs Editors Community] | |||
== 從文章內容,擷取網址中的網域部分 == | == 從文章內容,擷取網址中的網域部分 == | ||
Revision as of 14:06, 5 July 2023
從文章內容中擷取網址 (又稱 統一資源定位符, Uniform Resource Locator)。
從文章內容,擷取完整網址
使用 Google sheet 擷取完整網址
使用 Google 試算表正規表示法 (Regular expression) 的 REGEXEXTRACT 函數,從文章內容擷取第一個網址。
=REGEXEXTRACT(A1, "(http[s]?://[a-zA-Z0-9\-_\\._~\:\/\?#\[\]@\!\$&'\(\)\*\+,;\=%]+)")
使用 Sublime Text 擷取完整網址
使用 Sublime Text 等支援 regular expression 的文字編輯器
- 選單 Find --> Replace
- 啟用 Regular expression
- Find What: .*(http[s]?://[a-zA-Z0-9\-_\\._~\:\/\?#\[\]@\!\$&'\(\)\*\+,;\=%]+).*
此處輸入資料是一行只有一個網址。如果有多個網址,將會截取靠近行尾的網址。 - Replace with: \1
使用 Microsoft Excel 擷取完整網址
使用 Excel FIND, LEN 與 MID 等函數。資料限制:網址前後需要間隔空白或換行符號。以下公式從 B2 儲存格擷取完整網址:(公式修改自 guitarthrower 提供的公式[1])
=IF(ISERROR(MID(SUBSTITUTE(B2, "
", " "),FIND("http",SUBSTITUTE(B2, "
", " ")),IFERROR(FIND(" ",SUBSTITUTE(B2, "
", " "),FIND("http",SUBSTITUTE(B2, "
", " ")))-1,LEN(SUBSTITUTE(B2, "
", " ")))-FIND("http",SUBSTITUTE(B2, "
", " "))+1)), "", MID(SUBSTITUTE(B2, "
", " "),FIND("http",SUBSTITUTE(B2, "
", " ")),IFERROR(FIND(" ",SUBSTITUTE(B2, "
", " "),FIND("http",SUBSTITUTE(B2, "
", " ")))-1,LEN(SUBSTITUTE(B2, "
", " ")))-FIND("http",SUBSTITUTE(B2, "
", " "))+1))
測試資料
輸入資料: 不包含 HTML 語法的 a href 屬性標籤
Yahoo! 新聞 https://tw.news.yahoo.com/abc
輸出資料:
https://tw.news.yahoo.com/abc
說明:
- 網址可能是 http:// 或 https:// 開頭,所以條件是 http[s]?://
- 根據 RFC 3986 的 Section 2: Characters 網址允許的文字有 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=,其他文字則需要加上比例符號 % 編碼。 [2]
從 HTML 文字,擷取完整網址
使用 Google sheet 擷取完整網址
- Using EXTRACT URLs to extracts links and converts them to the HYPERLINK formula.
- Using FORMULATEXT function - Google Docs Editors Help
- Using REGEXEXTRACT function to extract the Url from above cell
=REGEXEXTRACT(A1, "(http[s]?://[a-zA-Z0-9\-_\\._~\:\/\?#\[\]@\!\$&'\(\)\*\+,;\=%]+)")
參考資料:
從文章內容,擷取網址中的網域部分
使用 Google sheet 擷取網域
使用 Google 試算表 REGEXEXTRACT 函數
=REGEXEXTRACT(A1, "(http[s]?\://[^/]+)")
輸入:
Yahoo! 新聞 https://tw.news.yahoo.com/abc
輸出:
https://tw.news.yahoo.com
說明:
- 網域指以 http:// 或 https:// 開頭,與相臨不是符號 / 的多個文字:[^/]+。
從文章內容,擷取特定檔案類型的網址
使用 Sublime Text 擷取特定檔案類型的網址
以下語法適用於 Sublime Tex
步驟1: 擷取該網頁的全部網址
- Chrome
瀏覽器安裝 Video Downloader GetThemAll 擴充套件 - 安裝後,點選工具列上的 Video Downloader GetThemAll 按鈕
- 點選「save link in txt」
- 儲存網址清單為純文字檔
步驟2: 刪除不包含不包含檔案類型的行,下例是檔案類型 .ttf
- 用 Sublime Tex 開啟網址清單,範例檔案如下:
Frequently Asked Questions http://www.clearchinese.com/faq.htm Contact Us http://www.clearchinese.com/contact.php HDZB_5 http://www.clearchinese.com/images/fonts/HDZB_5.TTF HDZB_6 http://www.clearchinese.com/images/fonts/HDZB_6.TTF
- 選單 Find --> Replace
- 啟用 Regular expression
- Find What: ^((?!\.ttf).)*$
此處語法是尋找不包含 .ttf 的行,可再修正為結尾不是 .ttf 的行。 - Replace with: (不需要輸入任何文字)
步驟3: 刪除空白行
- 選單 Find --> Replace
- 啟用 Regular expression
- Find What: ^[\s\t]*$\n
- Replace with: (不需要輸入任何文字)
步驟4: 只留下網址部分,刪除該行最前面的文字
- 選單 Find --> Replace
- 啟用 Regular expression
- Find What: .*(http[s]?://[a-zA-Z0-9\-_\\._~\:\/\?#\[\]@\!\$&'\(\)\*\+,;\=%]+)(\.ttf$)
- Replace with: \1\2
- 儲存網址清單,範例檔案如下:
http://www.clearchinese.com/images/fonts/HDZB_5.TTF http://www.clearchinese.com/images/fonts/HDZB_6.TTF
步驟5: 下載檔案
- 安裝與執行 Orbit Downloader
- 選單: 檔案 --> 匯入下載清單 --> 選擇網址清單
- 啟動下載任務
資料驗證用
文章內容是否包含網址
使用 Google 試算表 REGEXMATCH 函數,符合正規表示法的規則的話,回傳 TRUE。若不符合,則回傳 FALSE。
=REGEXMATCH(A1, "http")
輸入1:
Yahoo! 新聞 https://tw.news.yahoo.com/abc
輸出1:
TRUE
輸入2:
Yahoo! 新聞
輸出2:
FALSE
文章內容是否包含網域
原始資料包含網域,但是網域前面不包含 http e.g. tw.news.yahoo.com 或 www.bbc.co.uk。使用 Google 試算表 REGEXMATCH 函數,符合正規表示法的規則的話,回傳 TRUE。若不符合,則回傳 FALSE。
以下語法未處理 IPv4 形式的網域。(如果網域前面包含 http ,則可直接搜尋關鍵字: regular expression extract host )
=IF(ISERROR(REGEXMATCH(A1, "([a-zA-Z0-9\-_\\._~\:\/\?#\[\]@\!\$&'\(\)\*\+,;\=%]+\.[a-zA-Z]{2,}$|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})")), FALSE, REGEXMATCH(A1, "([a-zA-Z0-9\-_\\._~\:\/\?#\[\]@\!\$&'\(\)\*\+,;\=%]+\.[a-zA-Z]{2,}$|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"))
輸入1:
www.bbc.co.uk
輸出1:
TRUE
輸入2:
127.0.0.0
輸出2:
TRUE
輸入3:
Yahoo! 新聞
輸出3:
FALSE
不建議的其他方法:
- 檢查網域結尾是否是 .com, .tw, .net, .org 因為太多要列舉,該方法沒有效率。