Powershell - 将 ASP 页面的源代码分配给变量

发布于 2024-12-06 22:54:40 字数 1000 浏览 1 评论 0原文

也许这是一个愚蠢的问题,但到目前为止我在谷歌中找不到答案。 我需要阅读页面的 HTML 源代码 http://Mysite/ViewRequest.aspx?request_id=xxx。 我已经进行了设置,

wc = new-object -com internetexplorer.application
$wc.visible = $false
$wc.navigate2($strGRSpage).Document
$wc.silent = $true
$wc.visible = $false
$GRSstr = $wc.Document.body.IHTMLElement_outerText

不幸的是,即使我设置了 $wc.silent = $true 和 $wc.visible = $false,也会弹出 IE 页面。 是否有另一种方法(即使使用 HTTP 请求 GET)来检索该数据而不获取 IE 页面。 笔记。该网站 http://Mysite/ViewRequest.aspx?request_id=xxx 仅支持 IE 和 Mozilla。当我尝试使用 Webclient 类时,我总是得到不受支持的浏览器。

在 VBscript 中,以下作品(但我希望在 Powershell 中)

Set oHTML = CreateObject("Microsoft.XMLHTTP")  
Set oWeb = CreateObject("InternetExplorer.Application")  
oHTML.open "GET" , strGRSpage, false 
oHTML.send  
strGRStext = oHTML.responseText  

非常感谢,

Marco

probably it is a silly question, but I couldn't find the answer in Google so far.
I need to read the HTML source code of a page
http://Mysite/ViewRequest.aspx?request_id=xxx.
I've setup following

wc = new-object -com internetexplorer.application
$wc.visible = $false
$wc.navigate2($strGRSpage).Document
$wc.silent = $true
$wc.visible = $false
$GRSstr = $wc.Document.body.IHTMLElement_outerText

Unfortunately the IE page pops up, even if I set $wc.silent = $true and $wc.visible = $false.
Is there another way (even with HTTP request GET) to retrieve that data without getting the IE page.
NOTE. The site http://Mysite/ViewRequest.aspx?request_id=xxx supports just IE and Mozilla. When I tried to use Webclient Class I get always unsupported browser.

In VBscript following works (but I want it in Powershell)

Set oHTML = CreateObject("Microsoft.XMLHTTP")  
Set oWeb = CreateObject("InternetExplorer.Application")  
oHTML.open "GET" , strGRSpage, false 
oHTML.send  
strGRStext = oHTML.responseText  

Many thanks,

Marco

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画中仙 2024-12-13 22:54:40

您只需添加适当的 user-agent 标头。

例如:

$wc = new-object system.net.webclient
$wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
$wc.DownloadFile("http://whatsmyuseragent.com/","d:\index.html")

You just have to add an appropriate user-agent header.

For example:

$wc = new-object system.net.webclient
$wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
$wc.DownloadFile("http://whatsmyuseragent.com/","d:\index.html")

当尝试 webclient 类时,您可能只是缺少添加“有效”用户代理

$wc.Headers.Add("user-agent", "Valid WebClient Header")

 

When trying the webclient Class maybe You're just missing to add a "valid" user Agent

$wc.Headers.Add("user-agent", "Valid WebClient Header")

吻安 2024-12-13 22:54:40

经过一番谷歌搜索后,我发现了如何设置我的用户代理以获得与 IE 的兼容性。

Final code is 
$wc = new-object system.net.webclient
$wc.Headers.Add("user-agent", "Windows-RSS-Platform/2.0 (MSIE 9.0; Windows NT 6.1)")
$GRSstr = $wc.DownloadString($strGRSpage) 

非常感谢您的帮助。

问候,
Marco

----- 请标记为答案 ------

After Googling a little bit I found out how to setup my user agent to get the compatibility with IE.

Final code is 
$wc = new-object system.net.webclient
$wc.Headers.Add("user-agent", "Windows-RSS-Platform/2.0 (MSIE 9.0; Windows NT 6.1)")
$GRSstr = $wc.DownloadString($strGRSpage) 

Many thanks for your help.

Regards,
Marco

----- Please mark as answer ------

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文