PowerShell 抓取網(wǎng)頁(yè)表格

因?yàn)槲沂怯⑽陌娴南到y(tǒng),中文系統(tǒng)的界面轉(zhuǎn)換成字符串都成了亂碼方庭,因此測(cè)試都是在英文網(wǎng)頁(yè)上操作的。

PowerShell 5里面有一個(gè)新的函數(shù)叫做ConvertFrom-String, 他的作用是把字符串轉(zhuǎn)換成對(duì)象酱固。其中一個(gè)參數(shù)是可以根據(jù)指定的模板械念,把對(duì)應(yīng)的那一部分字符串匹配出來生成對(duì)象,我們可以利用這個(gè)功能抓取網(wǎng)頁(yè)中的表格运悲。

首先看個(gè)基本例子

<pre class="public-DraftStyleDefault-pre" data-offset-key="e3a03-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(18, 18, 18); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

<pre class="Editable-styled" data-block="true" data-editor="4naj0" data-offset-key="e3a03-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

a= @' 1 2 3 4 5 6 7 8 9 2 2 3 '@t=
@'
{Co1:1} {Co2:2} {Co3:3} {Co4:4}
{Co1
:5} 6 7 8
'@
c=a | ConvertFrom-String -Delimiter "\r\n"
d=a | ConvertFrom-string -TemplateContent $t

</pre>

</pre>

同樣的字符串龄减,第一個(gè)我用分隔符回車換行來生成一個(gè)對(duì)象;第二個(gè)我用自定義的模板格式來進(jìn)行匹配班眯。注意屬性定義的格式寫法 {}隔開希停,然后第一個(gè)需要{屬性名字:},后面不需要加,至少需要匹配2行數(shù)據(jù)才行署隘。

可以看見第一個(gè)對(duì)象有3個(gè)屬性宠能,P1是1 2 3 4,P2 是 4 5 6 7 磁餐,P3是9 2 2 3;

第二個(gè)對(duì)象則是根據(jù)每一列來自動(dòng)匹配的(已經(jīng)有一個(gè)模板匹配了前2行)

[圖片上傳失敗...(image-88d472-1613784436944)]

接下來我們來看2個(gè)實(shí)例。

第一個(gè)例子是這個(gè)網(wǎng)頁(yè)诊霹,里面有一個(gè)澳洲代理服務(wù)器的列表羞延,如下所示,我想抓出來

<pre class="public-DraftStyleDefault-pre" data-offset-key="1jo7c-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(18, 18, 18); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

<pre class="Editable-styled" data-block="true" data-editor="4naj0" data-offset-key="1jo7c-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

http://www.proxylisty.com/country/Australia-ip-list

</pre>

</pre>

[圖片上傳失敗...(image-9b7f9f-1613784436944)]

基本思路:invoke-restmethod直接抓取整個(gè)網(wǎng)頁(yè)脾还,自動(dòng)轉(zhuǎn)換為string對(duì)象伴箩。

然后設(shè)計(jì)對(duì)應(yīng)的模板。因?yàn)槭莌tml文件鄙漏,轉(zhuǎn)換為string以后對(duì)應(yīng)的html代碼都在里面嗤谚。因此關(guān)鍵是怎么把這些帶有html代碼的表格模板弄出來。

很簡(jiǎn)單泥张,網(wǎng)頁(yè)都可以查看html的源代碼呵恢,下面一大段html的代碼可以直接從網(wǎng)頁(yè)上復(fù)制粘貼對(duì)應(yīng)的2行表格代碼即可,稍加修改添加屬性名字就行了媚创。

然后根據(jù)模板匹配就會(huì)自動(dòng)生成對(duì)應(yīng)的表格對(duì)象了

<pre class="public-DraftStyleDefault-pre" data-offset-key="6f9u-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(18, 18, 18); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

<pre class="Editable-styled" data-block="true" data-editor="4naj0" data-offset-key="6f9u-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

web = 'http://www.proxylisty.com/country/Australia-ip-list'template =
@'
<tr>
<td>{IP:203.56.188.145}</td>
<td><a title='Port 8080 Proxy List'>{Port:8080}</a></td>
<td>HTTP</td>
<td><a style='color:red;' href='http://www.proxylisty.com/anonymity/High anonymous / Elite proxy-ip-list' title='High anonymous / Elite proxy Proxy List'>High anonymous / Elite proxy</a></td>
<td>No</td>
<td><a title='Australia IP Proxy List'><img style='margin: 0px 5px 0px 0px; padding: 0px;' src='http://www.proxylisty.com/assets/flags/AU.png' title='Australia IP Proxy List'/>Australia</a></td>
<td>13 Months</td>
<td>2.699 Sec</td>
<td><div id="progress-bar" class="all-rounded">
<div title='50%' id="progress-bar-percentage" class="all-rounded" style="width: 50%">{Reliability:50%}</div></div></td>
</tr>
<tr>
<td>{IP
:103.25.182.1}</td>
<td><a title='Port 8081 Proxy List'>{Port:8081}</a></td>
<td>HTTP</td>
<td><a style='color:red;' href='http://www.proxylisty.com/anonymity/Anonymous proxy-ip-list' title='Anonymous proxy Proxy List'>Anonymous proxy</a></td>
<td>No</td>
<td><a title='Australia IP Proxy List'><img style='margin: 0px 5px 0px 0px; padding: 0px;' src='http://www.proxylisty.com/assets/flags/AU.png' title='Australia IP Proxy List'/>Australia</a></td>
<td>15 Months</td>
<td>7.242 Sec</td>
<td><div id="progress-bar" class="all-rounded">
<div title='55%' id="progress-bar-percentage" class="all-rounded" style="width: 55%">{Reliability:55%}</div></div></td>
</tr>
'@
temp=Invoke-RestMethod -uriweb
result = ConvertFrom-String -TemplateContenttemplate -InputObject tempresult | sort reliability

</pre>

</pre>

成功抓取

image

我還可以更進(jìn)一步渗钉,我想測(cè)試一下這些抓取下來的地址是否真的可以用,寫個(gè)function測(cè)試看看

<pre class="public-DraftStyleDefault-pre" data-offset-key="132b-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(18, 18, 18); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

<pre class="Editable-styled" data-block="true" data-editor="4naj0" data-offset-key="132b-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

function Test-Proxy{
[cmdletbinding()]
param(
[Parameter(Mandatory=true, ValueFromPipeline=true,
ValueFromPipelineByPropertyName=true, position=0 ) ] [string]server,
[string]url = "http://www.microsoft.com" ) write-host "Test Proxy Server:server" -NoNewline
proxy = new-object System.Net.WebProxy(server)
WebClient = new-object System.Net.WebClientWebClient.proxy = proxy Try {content = WebClient.DownloadString(url)
Write-Host " Opened url successfully" -ForegroundColor Cyan } catch { Write-Host " Unable to accessurl" -ForegroundColor Yellow
}
}
foreach (r inresult){
servername="http://"+r.IP+":"+r.Port Test-proxy -serverservername -url "www.google.com"
}

</pre>

</pre>

測(cè)試標(biāo)明都是坑貨

image

類似的,豆子最近比較關(guān)注健康食物鳄橘,我想看看低GI的食物有哪些

<pre class="public-DraftStyleDefault-pre" data-offset-key="6v8j6-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(18, 18, 18); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

<pre class="Editable-styled" data-block="true" data-editor="4naj0" data-offset-key="6v8j6-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

http://ultimatepaleoguide.com/glycemic-index-food-list

</pre>

</pre>

需要把下面這個(gè)表格抓出來

image

<pre class="public-DraftStyleDefault-pre" data-offset-key="chnbs-0-0" style="margin: 1.4em 0px; padding: 0.88889em; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: auto; background: rgb(246, 246, 246); border-radius: 4px; color: rgb(18, 18, 18); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

<pre class="Editable-styled" data-block="true" data-editor="4naj0" data-offset-key="chnbs-0-0" style="margin: 0px; padding: 0px; font-size: 0.9em; word-break: normal; overflow-wrap: normal; white-space: pre; overflow: initial; background: rgb(246, 246, 246); border-radius: 0px;">

t2=@' <tr> <td valign="top">{Food*:Banana cake, made with sugar}</td> <td valign="top">{GI:47}</td> <td valign="top">{Size:60}</td> </tr> <tr> <td valign="top">{Food*:Banana cake, made without sugar}</td> <td valign="top">{GI:55}</td> <td valign="top">{Size:60}</td> </tr> '@web2='http://ultimatepaleoguide.com/glycemic-index-food-list/'
temp=Invoke-RestMethod -uriweb2
result1 = ConvertFrom-String -TemplateContentt2 -InputObject tempresult1 | Out-GridView

</pre>

</pre>

成功声离!

image

這種方式很有用,尤其是需要獲取網(wǎng)頁(yè)某些列表信息的時(shí)候瘫怜,當(dāng)然术徊,如果網(wǎng)頁(yè)本身就提供RESTFUL的接口,可以直接獲取JSON格式的內(nèi)容 那就更省事了鲸湃。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末赠涮,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子暗挑,更是在濱河造成了極大的恐慌笋除,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,496評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件炸裆,死亡現(xiàn)場(chǎng)離奇詭異垃它,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)烹看,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門国拇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人惯殊,你說我怎么就攤上這事酱吝。” “怎么了靠胜?”我有些...
    開封第一講書人閱讀 162,632評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵掉瞳,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我浪漠,道長(zhǎng)陕习,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,180評(píng)論 1 292
  • 正文 為了忘掉前任址愿,我火速辦了婚禮该镣,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘响谓。我一直安慰自己损合,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,198評(píng)論 6 388
  • 文/花漫 我一把揭開白布娘纷。 她就那樣靜靜地躺著嫁审,像睡著了一般。 火紅的嫁衣襯著肌膚如雪赖晶。 梳的紋絲不亂的頭發(fā)上律适,一...
    開封第一講書人閱讀 51,165評(píng)論 1 299
  • 那天辐烂,我揣著相機(jī)與錄音,去河邊找鬼捂贿。 笑死纠修,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的厂僧。 我是一名探鬼主播扣草,決...
    沈念sama閱讀 40,052評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼颜屠!你這毒婦竟也來了辰妙?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,910評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤汽纤,失蹤者是張志新(化名)和其女友劉穎上岗,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蕴坪,經(jīng)...
    沈念sama閱讀 45,324評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,542評(píng)論 2 332
  • 正文 我和宋清朗相戀三年敬锐,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了背传。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,711評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡台夺,死狀恐怖径玖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情颤介,我是刑警寧澤梳星,帶...
    沈念sama閱讀 35,424評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站滚朵,受9級(jí)特大地震影響冤灾,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜辕近,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,017評(píng)論 3 326
  • 文/蒙蒙 一韵吨、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧移宅,春花似錦归粉、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至浅乔,卻和暖如春倔喂,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工滴劲, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留攻晒,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,722評(píng)論 2 368
  • 正文 我出身青樓班挖,卻偏偏與公主長(zhǎng)得像鲁捏,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子萧芙,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,611評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容