如何在 PowerShell 中使用 Invoke-WebRequest 獲得網站連結?


要使用 PowerShell 獲取網站上的連結,我們可以首先使用 Invoke-WebRequest cmdlet 從網頁中檢索資料。

$req = Invoke-WebRequest -uri "https://theautomationcode.com"
$req

輸出

要僅檢索連結,我們可以使用該屬性,在那裡您還會找到一些子屬性,如 InnerHTML、Innertext、href 等,如輸出所示。

$req = Invoke-WebRequest -uri "https://theautomationcode.com"
$req.Links

輸出

innerHTML : Scripts
innerText : Scripts
outerHTML : <A href="https://theautomationcode.com/scripts/">Scripts</A>
outerText : Scripts
tagName   : A
href      : https://theautomationcode.com/scripts/  

我們只需要連結,所以我們將使用 href 屬性。

$req.Links | Select -ExpandProperty href

輸出

https://theautomationcode.com/2020/11/
https://theautomationcode.com/author/chiragce17/
https://theautomationcode.com/category/powershell/
https://theautomationcode.com/category/troubleshooting/

更新時間:2021 年 1 月 18 日

4K+ 瀏覽次數

開啟你的 職業之旅

完成課程,取得證書

開始學習
廣告