在 URL 中作为参数传递的特殊字符

Special characters passed as parameters in URL

要求:需要处理特殊字符的方法,如 %&。需要调整下面的代码,以便按原样处理来自 $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

正如 Biffen 所指出的,Web 服务器会将特殊字符视为代码。因此,在您的情况下,需要修改 $control 以便 Web 服务器了解您要前往的位置。

修复它的一种方法是在您要查找的原始产品名称中查找特定字符,并将它们替换为服务器可以理解的内容:

完整代码如下:

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

        $s = $control -replace '%','%25'
        $s = $s -replace ' ','+'
        $s = $s -replace '&','%26'
        $s = $s -replace '/','%2F'
        $site = $ie.Navigate("https://www.xxxy.com/search/all?name=$s")

        while ($ie.Busy -and $ie.ReadyState -ne 4){ sleep -Milliseconds 100 }

        $link = $null
        $link = $ie.Document.get_links() | where-object {if ($_.innerText){$_.innerText.contains($control)}}
        $link.click()
        while ($ie.Busy){ sleep -Milliseconds 100 }

        $ie.Document.body.outerHTML > d:\TEMP\med$control.txt
    }
}

$controls = "Accustix Glucose & Protein"

getStringMatch

我尝试使用以下字符串:

"3d1% Gel(30g)"
"Ace Revelol(25/2mg)"
"Accustix Glucose & Protein"
"32care Mouth/Throat"

您想 URL 编码 URI。在最开始添加:

Add-Type -AssemblyName 'System.Web'

然后像这样编码 URL:

$controlUri = [System.Web.HttpUtility]::UrlEncode($control)
$site = $ie.Navigate("https://www.xxxy.com/search/all?name=$controlUri")