Powershell-查看网站源信息 [英] Powershell - View Website Source Information

查看:102
本文介绍了Powershell-查看网站源信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些网站(例如gmail.com)不显示源信息(即,您无法右键单击并选择查看源")

因此,我试图将文档源读取到文件中,以便可以看到不同类型的元素(我希望最终能够将凭据和其他数据传递到网站中),但是遇到了困难. /p>

这是代码:

$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://www.gmail.com")
$ie.visible=$true
$doc = $ie.document
Add-Content C:\output.txt $doc.all

C:\ output.txt为空白,请帮助!

解决方案

使用InternetExplorer.Application的问题是您必须处理应用程序的行为,例如,如果我运行您的代码,我也会得到一个空文件,因为在访问文档属性后加载的页面.

如果使用的是Powershell v3,则可以使用Invoke-WebRequest cmdlet直接查询Web服务器,如下所示:

$webreq = Invoke-WebRequest http://www.gmail.com
$webreq.Content |Out-File C:\temp\output.txt

在powershell v2中,您可以按以下方式使用System.Net.Webrequest .NET类:

$req = [System.Net.WebRequest]::Create("http://www.gmail.com/")
$resp = $req.GetResponse()
$reqstream = $resp.GetResponseStream()
$stream = new-object System.IO.StreamReader $reqstream
$result = $stream.ReadToEnd()
$result | out-file c:\temp\output2.txt

There are some websites, such as gmail.com that don't display source information (i.e. you cannot right-click and select "View Source")

So I am trying to read the document source into a file so I can see the different types of elements (I would like to be able to pass credentials and other data into websites eventually), but I'm having difficulty.

Here is the code:

$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://www.gmail.com")
$ie.visible=$true
$doc = $ie.document
Add-Content C:\output.txt $doc.all

C:\output.txt is blank, help!

解决方案

The problem with using InternetExplorer.Application is you then have to handle the application behaviour, for example if I run your code I also get an empty file, because the page loaded after the document property was accessed.

If you are using Powershell v3, you can use the Invoke-WebRequest cmdlet to directly query the webserver as follows:

$webreq = Invoke-WebRequest http://www.gmail.com
$webreq.Content |Out-File C:\temp\output.txt

In powershell v2 you can use the System.Net.Webrequest .NET class as follows:

$req = [System.Net.WebRequest]::Create("http://www.gmail.com/")
$resp = $req.GetResponse()
$reqstream = $resp.GetResponseStream()
$stream = new-object System.IO.StreamReader $reqstream
$result = $stream.ReadToEnd()
$result | out-file c:\temp\output2.txt

这篇关于Powershell-查看网站源信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆