【问题标题】:How to pull specific information from a website using powershel如何使用 powershell 从网站中提取特定信息
【发布时间】:2020-03-05 15:29:33
【问题描述】:

如何从特定信息中提取信息?

我一直在尝试使用 Invoke-WebRequest 提取信息,但无法弄清楚如何让脚本仅从网站的特定部分提取数据。

例如,如果我只想从“版本”部分中提取信息 https://en.m.wikipedia.org/wiki/PowerShell我怎么能这样?

$website = Invoke-WebRequest -URI ‘https://en.m.wikipedia.org/wiki/PowerShell’

请告诉我如何做到这一点,非常感谢

【问题讨论】:

标签: powershell scripting


【解决方案1】:

对于您的 URI,遍历元素或形成特定元素。

$url = 'https://en.m.wikipedia.org/wiki/PowerShell'

($FormElements = Invoke-WebRequest -Uri $url -SessionVariable fe)   

<#
# Results

StatusCode        : 200
StatusDescription : OK
Content           : <!DOCTYPE html>
                    <html class="client-nojs" lang="en" dir="ltr">
                    <head>
...
#>


$FormElements.AllElements
<#
# Results

innerHTML : 
innerText : 
outerHTML : 
outerText : 
tagName   : !

innerHTML : <HEAD><TITLE>PowerShell - Wikipedia</TITLE>
            <META charset=UTF-8>
            <SCRIPT>document.documentElement.className="client-js
...#>    

$FormElements.ParsedHtml.anchors | Select 'ie8_href'
<#
# Results

ie8_href                                                                              
--------                                                                              
about:/w/index.php?title=Special:UserLogin&returnto=PowerShell                        
about:/w/index.php?title=PowerShell&action=edit&section=0                             
about://en.wikipedia.org/w/index.php?title=PowerShell&mobileaction=toggle_view_desktop
#>


($Form = $FormElements.Forms[0]) | Format-List -Force

<#
# Results

Id     : 
Method : get
Action : /w/index.php
Fields : {[main-menu-input, on], [searchInput, ]}
#>

$Form | Get-Member

<#
# Results

        TypeName: Microsoft.PowerShell.Commands.FormObject

Name        MemberType   Definition                                                        
----        ----------   ----------                                                        
Equals      Method       bool Equals(System.Object obj)                                    
GetHashCode Method       int GetHashCode()                                                 
GetType     Method       type GetType()                                                    
ToString    Method       string ToString()                                                 
Action      Property     string Action {get;}                                              
Fields      Property     System.Collections.Generic.Dictionary[string,string] Fields {get;}
Id          Property     string Id {get;}                                                  
Method      Property     string Method {get;} 
#>

$Form.Fields

<#
# Results

Key             Value
---             -----
main-menu-input on   
searchInput  
#>

【讨论】:

    猜你喜欢
    • 2023-01-03
    • 2013-09-12
    • 1970-01-01
    • 2011-09-21
    • 2010-09-24
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    相关资源
    最近更新 更多