Menu

[55dae9]: / sandbox / CalledscrapeSandbox1.ps1  Maximize  Restore  History

Download this file

209 lines (196 with data), 7.2 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#
# scrapeSandbox1.ps1
#
# Invoke-WebResponse outputs: Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject
#
# short version
function scrapeCustomerIds ($result) {
#
# accepts the output of Invoke-WebRequest as it's only parameter:
#
# PS > scrapeCustomerIds $result <--- Microsoft.PowerShell.Commands.HtmlWebResponseObject
#
# returns a hashtable with the structure:
#
# key value
# ~~~ ~~~~~
# <cust id> <hashtable>
# 15554026 {id, name}
# ...where id is the key of top table (numeric cust id) and name is cust name
#
$custs = @{}
$result.Links | ForEach-Object {
$link = $_
$matches = $null
try {
if ($link.href -match '/customers/(\d+)') {
$tmp = @{}
$companyId = $matches[1]
$tmp."id" = $companyId
$tmp."name" = $y.outerText
$custs.$companyId = $tmp
}
} catch {
$FailedItem = $_.Exception
Write-Output "FailedItem: $FailedItem"
Write-Output $_.Exception | Format-List -Force
}
}
# Write-Output "$($custs.Count) customers..."
return $custs
}
function scrapeCompanyIDs ($result) {
#
# .getElementsByClassName
# .getElementsByTagName
# .getElementsById
#
# $r3.ParsedHtml.getElementsByTagName('a') | ForEach-Object {
#
# now let's store unique customer IDs and info in a hash (which will also have a hash) of hashes
#
$custs = @{}
#
# 'Name' will be the id of the customer, as defined by Syncro (numbers)
#
$result.Links | ForEach-Object {
#
# innerHTML
# innerText
# outerHTML
# outerText
# tagName
# class
# href
#
$link = $_
$matches = $null
# $x = $y.href -match '/customers/(\d+)'
if ($link.href -match '/customers/(\d+)') {
Write-Host ('-' * 40)
$tmp = @{}
Write-Host "1:"
$1
Write-Host "2:"
$2
Write-Host "x:"
$x
Write-Host "matches:"
$matches
Write-Host "Company:" $matches[1]
$companyId = $matches[1]
$tmp."id" = $companyId
Write-Host "innerHTML:" $link.innerHTML
Write-Host "innerText:" $link.innerText
Write-Host "outerHTML:" $link.outerHTML
Write-Host "outerText:" $link.outerText
$tmp."name" = $link.outerText
Write-Host "tagName: " $link.tagName
Write-Host "class: " $link.class
Write-Host "href: " $link.href
# add to hashtable
$custs.$companyId = $tmp
}
}
Write-Host "--------------------custs has:" $custs.Count
$custs.GetEnumerator() | Sort-Object Name | ForEach-Object {
$cust = $_
Write-Host "In cust:" $cust.Key
foreach ($k in $cust.Value) {
Write-Host "id: " $k.'id'
Write-Host "name:" $k.'name'
}
}
}
function scrapeIt ($response) {
############################################################
#
# setup
#
$scriptDir = "C:\ProgramData\Syncro\live\scripts\"
Set-Location -Path $scriptDir
#
############################################################
#
# put all asset ids in an array (@assets)
#
$customerAssets = @{} # hash of hashes, with the 'Name' of top level being customer ID
$inputKvps = @{} # hash
$assets = @() # array
#$test = $wr.ParsedHtml.getElementsByTagName("input")
#Write-Host "test: $test"
#
$response.ParsedHtml.getElementsByTagName("input") | ForEach-Object {
if ($_.id -eq 'asset_ids_') {
$outer = $_.outerHtml
Write-Host ('_-' * 40)
Write-Host "outerHtml: " $outer
Write-Host "inputKvps count:" $inputKvps.Count
#
$outerClean = $outer.replace("<","")
$outerClean = $outerClean.replace(">","")
$outerClean = $outerClean.replace('INPUT',"")
$outerClean = $outerClean.replace('"',"")
Write-Host "outerClean: " $outerClean
# need ';'?
$outerPairSeparators = '?',' ','&amp;' # can be more, comma separated
$splitOptions = [System.StringSplitOptions]::RemoveEmptyEntries
$outerPairs = $outerClean.split($outerPairSeparators,$splitOptions)
foreach ($kvp in $outerPairs) {
# Write-Host ('-' * 10)
# Write-Host "Outer Pair: $kvp"
$k, $v = $kvp.split('=')
Write-Host "[before] k: $k and v: $v"
$v = $v.replace('+',' ') # inserted to make good HTML encoding
#$k = $k.replace('+',' ') # inserted to make good HTML encoding
$v = $v.replace('%2C', ',')
Write-Host "[ after] k: $k and v: $v against: [" $inputKvps.$k "]"
# to add or not to add?
if (-Not $inputKvps.ContainsKey($k)) {
$inputKvps.Add($k, $v)
Write-Host ">>> Added key: $k and value: $v to inputKvps"
} else {
# key exists, but is the value the same?
if ($inputKvps.$k -ne $v) {
# both of the below: If the entry does not already exist it
# will be automatically added.
# values are different; update
$inputKvps.Set_Item($k, $v)
# can also do:
# $inputKvps.$k = $v
Write-Host ">>> Updated key: $k to value: $v"
}
}
#
# easier than all the above (will update or add as required):
#
# $inputKvps.$k = $v
#
# to debug:
#
# these unwrap to a collection of DictonaryEntry objects
# $inputKvps.GetEnumerator() | ForEach-Object { … } ...or...
# ForEach($item in $inputKvps.GetEnumerator()) {Echo $item … }
# ForEach($item in $inputKvps.KEYS.GetEnumerator()) {Echo $item … }
$inputKvps.GetEnumerator() | ForEach-Object {
Write-Host " Item k:" $_.Key "v:" $_.Value
}
} # end of foreach ($kvp...
#$innerPairs = $outerPairs.split('=', $splitOptions)
#foreach ($x in $innerPairs) {
# Write-Host "Inner Pair: $x"
#}
#
$assets += $_.value
Write-Host "Added to assets: " $_.value
}
}
# now find asset name:
#$r3.ParsedHtml.getElementsByClassName('selectedId') | ForEach-Object {
# Write-Host "a: " $_
#}
# $classTest = $r3.ParsedHtml.getElementsByClass("table-entry-head")
# Write-Host "classTest: $classTest"
Write-Host "Assets:"
$assets
} # end function scrapeIt...
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.