2015-01-11 55 views
1

要求:需要處理特殊字符,如%&。需要調整下面的代碼,以便通過$Control文件來的特殊字符按原樣處理。在URL中傳遞的特殊字符作爲參數

例如:我有$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 
+3

有特殊字符的URL處理的一個事實上的標準的方式,這就是所謂的「[URL編碼] (https://en.wikipedia.org/wiki/Percent-encoding)」。 – Biffen

+0

你可以舉一個帶'&'符號的產品的例子嗎? –

+0

已經添加上面的問題的例子。 – Powershel

回答

3

你想URL編碼的URI。在添加此劈頭:

Add-Type -AssemblyName 'System.Web' 

然後編碼這樣的網址:

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

嗨 - 道歉,我想你只需要編碼$控制位,而不是整個URL。我已經編輯了上面的解決方案。 – Antony

+0

謝謝。這起到魅力。 – Powershel

1

正如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" 
+0

謝謝。它完美的作品。 – Powershel

相關問題