提取用双引号分隔的子字符串

时间:2017-08-20 02:41:47

标签: powershell web-scraping

此PowerShell脚本:((Invoke-WebRequest "http://www.gsmarena.com/battery-test.php3").AllElements | where class -eq "lalign").innerHTML

产生以下输出:

<A href="lenovo_p2-8319.php">Lenovo P2</A>
<A href="gionee_marathon_m5-7259.php">Gionee Marathon M5</A>
<A href="xiaomi_mi_max_2-8582.php">Xiaomi Mi Max 2</A>
<A href="asus_zenfone_max_zc550kl-7476.php">Asus Zenfone Max ZC550KL</A>
<A href="xiaomi_redmi_note_4-8531.php">Xiaomi Redmi Note 4</A>
<A href="samsung_galaxy_a7_(2017)-8335.php">Samsung Galaxy A7 (2017)</A>
<A href="samsung_galaxy_s6_active-7114.php">Samsung Galaxy S6 active</A>
<A href="xiaomi_mi_max-8057.php">Xiaomi Mi Max</A>
<A href="samsung_galaxy_j7_(2017)-8675.php">Samsung Galaxy J7 (2017)</A>
<A href="xiaomi_redmi_3-7862.php">Xiaomi Redmi 3</A>
<A href="nokia_lumia_1520-5760.php">Nokia Lumia 1520</A>
<A href="huawei_ascend_mate2_4g-5949.php">Huawei Ascend Mate2 4G</A>
<A href="sony_xperia_z4_tablet_lte-7069.php">Sony Xperia Z4 Tablet LTE</A>
<A href="xiaomi_redmi_3_pro-8007.php">Xiaomi Redmi 3 Pro</A>
<A href="xiaomi_mi_5s_plus-8350.php">Xiaomi Mi 5s Plus</A>
<A href="xiaomi_redmi_3s-8150.php">Xiaomi Redmi 3s</A>
<A href="samsung_galaxy_a9_(2016)-7641.php">Samsung Galaxy A9 (2016)</A>
<A href="huawei_mate_8-7239.php">Huawei Mate 8</A>
<A href="samsung_galaxy_j7_(2016)-7870.php">Samsung Galaxy J7 (2016)</A>
<A href="sony_xperia_z3_compact-6538.php">Sony Xperia Z3 Compact</A>
<A href="samsung_galaxy_tab_pro_10_1_lte-5941.php">Samsung Galaxy Tab Pro 10.1 LTE</A>
<A href="motorola_moto_z_play-8310.php">Motorola Moto Z Play</A>
<A href="samsung_galaxy_c7-8046.php">Samsung Galaxy C7</A>
<A href="meizu_m3_max-8321.php">Meizu M3 Max</A>
<A href="samsung_galaxy_a8_duos-7506.php">Samsung Galaxy A8 Duos</A>
<A href="meizu_m5_note-8450.php">Meizu M5 Note</A>
<A href="samsung_galaxy_a8-7249.php">Samsung Galaxy A8</A>
<A href="lg_g_flex-5806.php">LG G Flex</A>

但我只需要双引号之间的字符串。我该怎么办?

1 个答案:

答案 0 :(得分:0)

对于您的具体情况,这应该有效: 将整个字符串放在变量$ string中并执行以下行

$string.Split('"')|%{if($PSItem.ToString().EndsWith('.php')){Write-Host $PSItem}}