要求:需要一种方法来处理特殊字符,例如%
和&
。需要调整以下代码,以便按原样处理通过$Control
文件传入的特殊字符。
例如:我在$control
文件中有一个条目25% Dextrose(25ml)
。我需要一种方法,以便$ie.Navigate
应该简单地导航到https://www.xxxy.com/search/all?name=25% Dextrose(25ml)
。当前,它被路由到https://www.xxxy.com/search/all?name=25%% Dextrose(25ml)
(注意URL中的额外%),因此找不到该网页。
**Few examples of special characters that need to be tackled:**
'/' - 32care Mouth/Throat
'%' - 3d1% Gel(30g)
'&' - Accustix Glucose & Protein
'/' - Ace Revelol(25/(2.5mg)
function getStringMatch
{
# Loop through all 2 digit combinations in the $path directory
foreach ($control In $controls)
{
$ie = New-Object -COMObject InternetExplorer.Application
$ie.visible = $true
$site = $ie.Navigate("https://www.xxxy.com/search/all?name=$control")
$ie.ReadyState
while ($ie.Busy -and $ie.ReadyState -ne 4){ sleep -Milliseconds 100 }
$link = $null
$link = $ie.Document.get_links() | where-object {$_.innerText -eq "$control"}
$link.click()
while ($ie.Busy -and $ie.ReadyState -ne 4){ sleep -Milliseconds 100 }
$ie2 = (New-Object -COM 'Shell.Application').Windows() | ? {
$_.Name -eq 'Windows Internet Explorer' -and $_.LocationName -match "^$control"
}
# NEED outerHTML of new page. CURRENTLY it is working for some.
$ie.Document.body.outerHTML > d:\med$control.txt
}
}
$controls = "Sporanox"
getStringMatch
最佳答案
您要对URI进行URL编码。首先添加:
Add-Type -AssemblyName 'System.Web'
然后像这样编码URL:
$controlUri = [System.Web.HttpUtility]::UrlEncode($control)
$site = $ie.Navigate("https://www.xxxy.com/search/all?name=$controlUri")
关于regex - URL中作为参数传递的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27887655/