-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathhtml table webscrapping to json.ps1
More file actions
156 lines (143 loc) · 4.58 KB
/
html table webscrapping to json.ps1
File metadata and controls
156 lines (143 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
$rawdata = @'
</table>
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
<th>Extn.</th>
<th>E-mail</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger</td>
<td>Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
<td>5421</td>
<td>t.nixon@datatables.net</td>
</tr>
<tr>
<td>Garrett</td>
<td>Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
<td>8422</td>
<td>g.winters@datatables.net</td>
</tr>
<tr>
<td>Ashton</td>
<td>Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$86,000</td>
<td>1562</td>
<td>a.cox@datatables.net</td>
</tr>
<tr>
<td>Cedric</td>
<td>Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012/03/29</td>
<td>$433,060</td>
<td>6224</td>
<td>c.kelly@datatables.net</td>
</tr>
<tr>
<td>Airi</td>
<td>Satou</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td>2008/11/28</td>
<td>$162,700</td>
<td>5407</td>
<td>a.satou@datatables.net</td>
</tr>
</tbody>
</table>
'@
$th = $rawdata -split "`r`n"| where-object {$_ -match '<th>'}
$td = $rawdata -split "`r`n"| where-object {$_ -match '<td>'}
$thead = (($th -replace '<th>', '') -replace '</th>', '').trim()
$tdata = (($td -replace '<td>', '') -replace '</td>', '').trim()
$groupOf =
$result = New-Object System.Collections.ArrayList
for ($i = 0; $i -le $tdata.count; $i+= ($thead.count - 1))
{
if ($tdata.count -eq $i)
{
break
}
$group9 = $i + ($thead.count - 1)
#"{0} {1}" -f $i, $group9
[void]$result.Add($tdata[$i..$group9])
#$result = @($tdata[$i..$group9]) #-join ', '
$i++
}
$finalResult = @()
foreach ($data in $result)
{
$finalResult += [pscustomobject]@{
$thead[0] = $data[0]
$thead[1] = $data[1]
$thead[2] = $data[2]
$thead[3] = $data[3]
$thead[4] = $data[4]
$thead[5] = $data[5]
$thead[6] = $data[6]
$thead[7] = $data[7]
$thead[8] = $data[8]
}
}
$finalResult | ft -Auto
#$finalResult | ConvertTo-Json | Out-File c:\temp\database.json
#$finalResult | Export-Csv c:\temp\database.csv -NoTypeInformation
####################################################################################
####################################################################################
#Open website
$webSite = Invoke-WebRequest -Uri https://datatables.net/
#Find table in the website
$tableHeader = $webSite.AllElements | Where-Object {$_.tagname -eq 'th'}
$tableData = $webSite.AllElements | Where-Object {$_.tagname -eq 'td'}
#Table header and data
$thead = $tableHeader.innerText[0..(($tableHeader.innerText.count/2) - 1)]
$tdata = $tableData.innerText
#Break table data into smaller chuck of data.
$dataResult = New-Object System.Collections.ArrayList
for ($i = 0; $i -le $tdata.count; $i+= ($thead.count - 1))
{
if ($tdata.count -eq $i)
{
break
}
$group = $i + ($thead.count - 1)
[void]$dataResult.Add($tdata[$i..$group])
$i++
}
#Html data into powershell table format
$finalResult = @()
foreach ($data in $dataResult)
{
$newObject = New-Object psobject
for ($i = 0; $i -le ($thead.count - 1); $i++) {
$newObject | Add-Member -Name $thead[$i] -MemberType NoteProperty -value $data[$i]
}
$finalResult += $newObject
}
$finalResult | ft -AutoSize