英文:
fetching SQL product name using version from microsoft website - powershell / Invoke-webrequest
问题
我正在进行网站抓取,从微软的网站上获取SQL产品名称,例如"SQL Version 2012",使用它们的版本号"11.0.6607.3"。
基本上,我想使用他们的版本号来搜索他们的产品名称。请使用PowerShell和Invoke-WebRequest帮助我。
先谢谢了。
以下是我尝试过的内容:
$url = "https://learn.microsoft.com/en-us/troubleshoot/sql/releases/download-and-install-latest-updates"
$html.content = Invoke-WebRequest -Uri $url
# 在HTML中查找表格行(tr)
$rows = ($html.content).ParsedHtml.getElementsByTagName('tr')
# 定义要搜索的版本号
$searchVersion = '11.0.5058.0'
# 循环遍历行并查找匹配的版本号
foreach ($row in $rows) {
$cells = $row.getElementsByTagName('td')
if ($cells.Count -gt 1 -and $cells[1].innerText -match $searchVersion) {
# 版本号在行的第二个单元格中找到
# 从同一行的第一个单元格输出SQL Server版本
Write-Output $cells[0].innerText
break # 找到第一个匹配后退出循环
}
}
英文:
I working on scrapping microsoft website where i want to fetch sql product name such as "SQL Version 2012" using their version "11.0.6607.3".
Basically I want to search for their product name using their Version. Help me with powershell using invoke-webrequest.
Thanks in Advance
This is what I have tried
$url = "https://learn.microsoft.com/en-us/troubleshoot/sql/releases/download-and-install-latest-updates"
$html.content = Invoke-WebRequest -Uri $url
# Find the table rows (tr) in the HTML
$rows = ($html.content).ParsedHtml.getElementsByTagName('tr')
# Define the version number to search for
$searchVersion = '11.0.5058.0'
# Loop through the rows and find the matching version number
foreach ($row in $rows) {
$cells = $row.getElementsByTagName('td')
if ($cells.Count -gt 1 -and $cells[1].innerText -match $searchVersion) {
# The version number is found in the second cell of the row
# Output the SQL Server version from the first cell of the same row
Write-Output $cells[0].innerText
break # Exit the loop after the first match is found
}
}
答案1
得分: 1
这是一个快速且简单的方法(没有错误处理),可以在当前网址今天使用,但它很脆弱,如果页面布局发生变化,可能会出错...
$url = "https://learn.microsoft.com/en-us/troubleshoot/sql/releases/download-and-install-latest-updates";
$html = (Invoke-WebRequest -Uri $url -UseBasicParsing).Content;
$searchVersion = "11.0.5058.0";
# 找到版本文本
$index = $html.IndexOf($searchVersion);
# 找到包含的“<tr>”的开始位置
$tr = $html.LastIndexOf("<tr>", $index);
# 找到以下“<strong>...</strong>”内的文本
$start = $html.IndexOf("<strong>", $tr) + "<strong>".Length;
$end = $html.IndexOf("</strong>", $tr);
$name = $html.Substring($start, $end - $start);
$name
# SQL Server 2012
最好使用像HTML Agility Pack这样的正确的HTML解析库,但对于这样一个简单的用例,仅使用基本的字符串搜索可能就足够了。
请注意,在PowerShell“Core”中,ParsedHtml
属性不可用,所以如果你正在编写新代码,最好避免使用它,并添加-UseBasicParsing
开关,即使你目前是针对Windows PowerShell。
而且,由于SQL Server实际上并不会那么频繁地发布新版本,也许直接在脚本中硬编码一个哈希表来进行查找会更好...
英文:
Here's a quick and dirty way to do it (with no error handling) that works with that url today, but it's brittle and might break if the page layout changes...
$url = "https://learn.microsoft.com/en-us/troubleshoot/sql/releases/download-and-install-latest-updates";
$html = (Invoke-WebRequest -Uri $url -UseBasicParsing).Content;
$searchVersion = "11.0.5058.0";
# find the version text
$index = $html.IndexOf($searchVersion);
# find the start of the containing "<tr>"
$tr = $html.LastIndexOf("<tr>", $index);
# find the text inside the following "<strong>...</strong>"
$start = $html.IndexOf("<strong>", $tr) + "<strong>".Length;
$end = $html.IndexOf("</strong>", $tr);
$name = $html.Substring($start, $end - $start);
$name
# SQL Server 2012
It would probably be better to use a proper HTML parser library like the HTML Agility Pack, but for a simple use case like this it's probably enough to just do it with basic string searches.
Note that the ParsedHtml
property isn't available in PowerShell "Core" so if you're writing new code it's more future-proof if you avoid using it and add the -UseBasicParsing
switch, even if you're targeting Windows PowerShell at the moment.
And since SQL Server doesn't really get new versions that frequently, it even might be better to simply have a hashtable hardcoded in your script to do the lookups with...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论