【问题标题】:HTMLagilityPack in combination with Powershell, Windows authentificationHTMLagilityPack 与 Powershell、Windows 身份验证结合使用
【发布时间】:2017-10-25 09:18:31
【问题描述】:

所以我有一个名为 lansweeper 的工具。它在本地服务器上运行。现在我想从中抓取一个页面,但它使用 Windows 身份验证。 我使用 Powershell 作为脚本语言。 我主要使用 HTMLAgilityPack 来抓取。但我从来没有刮过使用 Windows 身份验证的页面。

有谁知道我如何通过它传递我的凭据?以便它在某些凭据下打开页面? (就像我的管理员帐户而不是我的普通帐户)。 (是的,我可以将我的普通用户添加到 Lansweeper 中的允许用户中,但这不是我想要使用的解决方案。

我尝试了以下方法,但它不起作用。

[Reflection.Assembly]::LoadFile("C:\Scraping\HtmlAgilityPack\lib\Net45\HtmlAgilityPack.dll”)
[HtmlAgilityPack.HtmlWeb]$web = @{}
$webclient = new-object System.Net.WebClient
$username = "user"
$password = "passw0rd-"
$domain = "mydomain"
$webclient.Credentials = new-object System.Net.NetworkCredential($username, $password, $domain)
[HtmlAgilityPack.HtmlDocument]$doc = $web.Load("http://lansweeper:81/user.aspx?username=sam&userdomain=mydomain","","",$webclient.Credentials) 
[HtmlAgilityPack.HtmlNodeCollection]$nodes = $doc.DocumentNode.SelectNodes("//body")

我一直在研究函数,发现了两种可能性:

TypeName   : HtmlAgilityPack.HtmlWeb
Name       : Load
HtmlAgilityPack.HtmlDocument Load(string url), 
HtmlAgilityPack.HtmlDocument Load(string url, string proxyHost, int proxyPort, string userId, string password), 
HtmlAgilityPack.HtmlDocument Load(string url, string method), 
HtmlAgilityPack.HtmlDocument Load(string url, string method, System.Net.WebProxy proxy, System.Net.NetworkCredential credentials)

Name       : Get
MemberType : Method
void Get(string url, string path), 
void Get(string url, string path, System.Net.WebProxy proxy, System.Net.NetworkCredential credentials), 
void Get(string url, string path, string method), 
void Get(string url, string path, System.Net.WebProxy proxy, System.Net.NetworkCredential credentials, string method)

但我无法让其中一个工作。有人用 Powershell 做过吗?

【问题讨论】:

    标签: powershell html-agility-pack


    【解决方案1】:

    我找到了方法:我希望它对将来的人有所帮助。 这不是直接弄清楚,但是一旦你看到它就很容易了。

    [Reflection.Assembly]::LoadFile("C:\temp\HtmlAgilityPack\lib\Net45\HtmlAgilityPack.dll") | Out-Null
    [HtmlAgilityPack.HtmlWeb]$web = @{}
    $url = "http://lansweeper:81/user.aspx?username=sam&userdomain=mydomain"
    $webclient = new-object System.Net.WebClient
    
        $cred = new-object System.Net.NetworkCredential
        $defaultCredentials =  $cred.UseDefaultCredentials
    
    $proxyAddr = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyServer
    $proxy = new-object System.Net.WebProxy
    $proxy.Address = $proxyAddr
    $proxy.useDefaultCredentials = $true 
    $proxy
    
    [HtmlAgilityPack.HtmlDocument]$doc = $web.Load($url,"GET","$proxy",$defaultCredentials ) 
    [HtmlAgilityPack.HtmlNodeCollection]$nodes = $doc.DocumentNode.SelectNodes("//html[1]/body[1]")
    
    $nodes
    
    <# USER RESOURCES
    https://msdn.microsoft.com/en-us/library/system.net.webclient.usedefaultcredentials(v=vs.110).aspx
    https://forums.asp.net/t/2027997.aspx?HtmlAgilityPack+Stuck+trying+to+understand+HtmlWeb+Load+NetworkCredential
    https://msdn.microsoft.com/en-us/library/system.net.webclient.usedefaultcredentials.aspx
    https://stackoverflow.com/questions/571429/powershell-web-requests-and-proxies
    
    TypeName   : HtmlAgilityPack.HtmlWeb
    Name       : Load
    HtmlAgilityPack.HtmlDocument Load(string url, string proxyHost, int proxyPort, string userId, string password), 
    HtmlAgilityPack.HtmlDocument Load(string url, string method, System.Net.WebProxy proxy, System.Net.NetworkCredential credentials)
    #>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-16
      • 2011-03-12
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      • 2013-01-04
      • 1970-01-01
      相关资源
      最近更新 更多