我已經相對成功地使用以下變體來抓取網路資料。
$url = “https://msrc.microsoft.com/update-guide/en-US/vulnerability/CVE-2019-1331"
$response = Invoke-WebRequest -Uri $url
$response.ParsedHtml.body.getElementsByClassName('css-247') | select -expand innertext
我無法獲取存盤在代碼中列出的 url 中的段落標記中的資料。
<div class="css-247">
<p>
'Text I want to copy
我假設 P 不是“內部文本”?我怎樣才能抓住那個文本?如果我將 XPath 復制到文本所在的位置:它位于/html/body/div/div/div/div/div[2]/div/div[2]/div/div[2]/div/div[2]/div[3]/div[2]/div/div/div/div/div/div/p[1]
如果我復制 cssPath 我得到:
html body.ms-Fabric--isFocusHidden div#root div.ms-Fabric.root-41 div.css-43 div.ms-Stack.css-87 div.ms-Stack.css-87 div.ms-ScrollablePane.root-88 div.ms-ScrollablePane--contentContainer.contentContainer-89 div.ms-Stack.css-93 div.ms-Stack.css-110 div.ms-ScrollablePane.root-88 div.ms-ScrollablePane--contentContainer.contentContainer-89 div div div#executiveSummary.ms-Stack.ms-Card.css-136 div.ms-Stack.ms-CardSection.css-138 div.ms-Shimmer-container.root-113 div.ms-Shimmer-dataWrapper.dataWrapper-140 div.ms-StackItem.ms-CardItem.css-246 div.css-247 p
uj5u.com熱心網友回復:
資料是從回傳 JSON 的 API 呼叫中動態檢索的,即當您使用瀏覽器導航到問題中的 URI 時,瀏覽器會運行 Javascript,這會導致發出額外的 XHR 請求并使用該內容更新頁面。使用您當前的方法,不會發出這些請求,因此不存在所需的內容。
您需要呼叫適當的端點,在瀏覽器的網路選項卡中找到,提取 JSON 回應的相關部分,從 html 中決議出摘要:
$url = "https://api.msrc.microsoft.com/sug/v2.0/en-US/vulnerability/CVE-2019-1331"
$response = Invoke-WebRequest -Uri $url
$data = $response | ConvertFrom-Json
$summary = $data.description
$html = New-Object -ComObject "HTMLFile"
$html.IHTMLDocument2_write($summary)
$html.firstChild | % innerText
我在這里查找了向 IHTMLDocument2 寫入 html 字串:https ://paullimblog.wordpress.com/2017/08/08/ps-tip-parsing-html-from-a-local-file-or-a-string/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/412058.html
標籤:
上一篇:所有活動的基本活動
下一篇:連接抓取的資料
