Powershell解析HTML系列以将其保存在CSV中

时间:2018-11-13 15:20:16

标签: html powershell parsing html-parsing

我正在尝试抓取一个网站,以便随着时间的推移自动跟踪我在Rocket League播放列表中的排名。我的想法是,我要抓住与播放列表相对应的数字,然后将其放入csv中以作参考。我已经能够获取该网页的HTML,但似乎无法有效地解析出我想要的数字。

这是我收集网页信息的方式:

$tracker = Invoke-WebRequest -Uri 
https://rocketleague.tracker.network/profile/steam/adammast12

$tracker.RawContent

这是我感兴趣的RawContent部分:

<script type="text/javascript">
$('#playlist-tracking-rating').highcharts({
    chart: {
        type: 'line',
        zoomType: 'xy'
    },
    title: {
        text: 'Rating Progression'
    },
    xAxis: {
        categories: ['Nov 05','Nov 08','Nov 10','Nov 11','Nov 12','Nov 13'],
        type: 'date',
    },
    yAxis: {
        title: {
            text: 'Rating'
        }
    },
    tooltip: {
        enabled: true,
        shared: true
    },
    plotOptions: {
        line: {
            dataLabels: {
                enabled: true
            },
            enableMouseTracking: true
        }
    },
    series: [

                { name: 'Un-Ranked', data: [1270,1270,1270,1270,1270,1251] },


                { name: 'Ranked Duel 1v1', data: [655,655,655,655,655,655] },


                { name: 'Ranked Doubles 2v2', data: [815,775,856,847,865,865] },


                { name: 'Ranked Solo Standard 3v3', data: [788,788,788,788,788,788] },


                { name: 'Ranked Standard 3v3', data: [994,994,994,994,994,994] },


                { name: 'Hoops', data: [556,556,556,556,525,525] },


                { name: 'Rumble', data: [651,741,703,703,704,704] },


                { name: 'Dropshot', data: [635,635,635,635,635,635] },


                { name: 'Snowday', data: [770,770] },

    ]
});

$('#playlist-tracking').highcharts({
    chart: {
        type: 'spline',
        zoomType: 'xy'
    },
    title: {
        text: 'Tier Over Time'
    },
    subtitle: {
        text: ''
    },
    xAxis: {
        categories: ['Nov 05','Nov 08','Nov 10','Nov 11','Nov 12','Nov 13'],
        type: 'date',
        labels: {
            overflow: 'justify'
        }
    },
    tooltip: {
        enabled: true,
        shared: true
    },
    yAxis: {
        title: {
            text: ''
        },
        labels: {
            enabled: false
        },
        minorGridLineWidth: 0,
        gridLineWidth: 0,
        alternateGridColor: null,
        plotBands: [{
            from: 0,
            to: 0.99,
            color: 'rgba(75, 75, 75, 0.1)',
            label: {
                text: 'Unranked',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 1,
            to: 1.99,
            color: 'rgba(227, 150, 68, 0.1',
            label: {
                text: 'Bronze I',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 2,
            to: 2.99,
            color: 'rgba(227, 150, 68, 0.1)',
            label: {
                text: 'Bronze II',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 3,
            to: 3.99,
            color: 'rgba(227, 150, 68, 0.1)',
            label: {
                text: 'Bronze III',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 4,
            to: 4.99,
            color: 'rgba(197, 197, 197, 0.1)',
            label: {
                text: 'Silver I',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 5,
            to: 5.99,
            color: 'rgba(197, 197, 197, 0.1)',
            label: {
                text: 'Silver II',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 6,
            to: 6.99,
            color: 'rgba(197, 197, 197, 0.1)',
            label: {
                text: 'Silver III',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 7,
            to: 7.99,
            color: 'rgba(206, 163, 32, 0.1)',
            label: {
                text: 'Gold I',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 8,
            to: 8.99,
            color: 'rgba(206, 163, 32, 0.1)',
            label: {
                text: 'Gold II',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 9,
            to: 9.99,
            color: 'rgba(206, 163, 32, 0.1)',
            label: {
                text: 'Gold III',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 10,
            to: 10.99,
            color: 'rgba(37, 161, 213, 0.1)',
            label: {
                text: 'Platinum I',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 11,
            to: 11.99,
            color: 'rgba(37, 161, 213, 0.1)',
            label: {
                text: 'Platinum II',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 12,
            to: 12.99,
            color: 'rgba(37, 161, 213, 0.1)',
            label: {
                text: 'Platinum III',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 13,
            to: 13.99,
            color: 'rgba(0, 79, 182, 0.1)',
            label: {
                text: 'Diamond I',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 14,
            to: 14.99,
            color: 'rgba(0, 79, 182, 0.1)',
            label: {
                text: 'Diamond II',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 15,
            to: 15.99,
            color: 'rgba(0, 79, 182, 0.1)',
            label: {
                text: 'Diamond III',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 15,
            to: 15.99,
            color: 'rgba(142, 89, 225, 0.1)',
            label: {
                text: 'Champion I',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 15,
            to: 15.99,
            color: 'rgba(142, 89, 225, 0.1)',
            label: {
                text: 'Champion II',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 15,
            to: 15.99,
            color: 'rgba(142, 89, 225, 0.1)',
            label: {
                text: 'Champion III',
                style: {
                    color: '#606060'
                }
            }
        }, {
            from: 15,
            to: 15.99,
            color: 'rgba(249, 135, 254, 0.1)',
            label: {
                text: 'Grand Champion',
                style: {
                    color: '#606060'
                }
            }
        }]
    },
    plotOptions: {
        spline: {
            lineWidth: 4,
            states: {
                hover: {
                    lineWidth: 5
                }
            },
            marker: {
                enabled: false
            }
        }
    },
    navigation: {
        menuItemStyle: {
            fontSize: '10px'
        }
    },
    series: [

                { name: 'Ranked Duel 1v1', data: [0,0,0,0,0,0] },


                { name: 'Ranked Doubles 2v2', data: [11,11,12,12,12,12] },


                { name: 'Ranked Solo Standard 3v3', data: [0,0,0,0,0,0] },


                { name: 'Ranked Standard 3v3', data: [0,0,0,0,0,0] },


                { name: 'Hoops', data: [0,0,0,0,0,0] },


                { name: 'Rumble', data: [0,11,11,11,11,11] },


                { name: 'Dropshot', data: [10,10,10,10,10,10] },


                { name: 'Snowday', data: [12,12] },


    ]
});

我想从与排名播放列表相关的系列中获取信息。例如,我需要能够获得与“ Ranked Duel 1v1”和“ Ranked Doubles 2v2”等相对应的第一个值,以便可以将该数字保存在csv中。

我尝试过按这样的字符串搜索:

$data = $tracker.tostring() -split "[`r`n]" | select-string "Ranked Standard 3v3"

这是给我的结果:

  Ranked Standard 3v3
Ranked Standard 3v3
Ranked Standard 3v3
Ranked Standard 3v3
Ranked Standard 3v3
Ranked Standard 3v3
                    { name: 'Ranked Standard 3v3', data: 
[994,994,994,994,994,994] },
                    { name: 'Ranked Standard 3v3', data: [0,0,0,0,0,0] },

我不确定如何从那里解析它。感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

以下是如何获取“未跟踪”部分的示例:

# result ArrayList
$results = New-Object System.Collections.ArrayList

# REST Get

$tracker = Invoke-WebRequest -Uri https://rocketleague.tracker.network/profile/steam/adammast12

# HTML data
# $tracker.RawContent

# split by carriage return + new line
# select the JSON with "name:" in it
$data = $tracker.tostring() -split "`r`n" | Select-String "name:"

# Un-Ranked data
$unranked = $data | Select-String "name: 'Un-Ranked'"

# Split at []'s

$unrankedSplit = $unranked.ToString().Split('[').Split(']')

# this yields a result like this:
#                     { name: 'Un-Ranked', data: 
# 1270,1270,1270,1270,1270,1251
# },
#
# Split again at the second position on each comma, position [1]
# since PowerShell is zero-based indexing


$unrankedSplitChild = $unrankedSplit[1].Split(',')

# loop through each item with custom objects
foreach($item in $unrankedSplitChild)
{
    # create a PSCustomObject and add to to the results
    $results += [PSCustomObject]@{Category="Un-Ranked";Data=$item}
}

# throw the results to the console
$results | Format-Table -AutoSize