Jump to content
Sign in to follow this  
Allow2010

need help parsing some html

Recommended Posts

Hello,

i have a router staus page (static routes) which looks like this (there may be hundreds in some cases):

<input type="checkbox" id="route0" name="route0" checked></td><td>7.7.9.9</td><td>255.255.255.255</td><td>192.168.0.0</td>
<input type="checkbox" id="route0" name="route0" ></td><td>7.7.9.9</td><td>255.255.255.255</td><td>192.168.0.0</td>

I would like to generate an array with the following content

stateofcheckbox
name
IP
subnet
gateway

for X lines like in the example above.

The array needs to store this info:

stateofcheckbox true
name route0
IP 7.7.9.9
subnet 255.255.255.255
gateway 192.168.0.0

and for line 2

stateofcheckbox false
name route0
IP 7.7.9.9
subnet 255.255.255.255
gateway 192.168.0.0

and so on.

Can anyone help me with parsing the html and also with constructing the array?

All ideas welcome 🙂 Thanks!

Edited by Allow2010

Share this post


Link to post
Share on other sites
28 minutes ago, Allow2010 said:

Can anyone help me with parsing the html and also with constructing the array?

Need help means you have something that doesn't work.... right?
So what do you have? :) 

Jos

 


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

I have an idea I want to share with you @Jos.   Recently, we see so many times ppl asking for help under a false request, in fact asking for the community to provide spoon-feeding software, free of charge scripts, slave programmatic efforts.  Do you think it is time for mods and devs to react to it like you would react for game-automation, etc ?  When you ask for basic code from the OP, no one should be allowed to provide code until some effort is made by OP ? 

Share this post


Link to post
Share on other sites

or a blanket requirement that anytime a mod is engaged, we all stand down?  And if they pinned that to the announcements?   :)  

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

@jos

Thanks for giving me the opportunity to give more details. I have a working solution, but it is really ugly and i was reluctant to post it.

If it helps as a starting point, here it is:

#include <Array.au3>

Dim $a_staticroutes[0][5]

$examplelines = '<input type="checkbox" id="route0" name="route0" checked></td><td>1.1.1.1</td><td>2.2.2.2</td><td>3.3.3.3</td>' & _
                '<input type="checkbox" id="route1" name="route1" ></td><td>4.4.4.4</td><td>5.5.5.5</td><td>6.6.6.6</td>' & _
                '<input type="checkbox" id="route2" name="route2" checked></td><td>7.7.7.7</td><td>8.8.8.8</td><td>9.9.9.9</td>'

$counter = 0
While 1

    ConsoleWrite("---------------" & @crlf)

    $name = TextBetween($examplelines, 'id="route', '" name="route', 0, 1+1*$counter, 0, 1+1*$counter)
    If @error then ExitLoop
    ConsoleWrite("Name: " & $name & @CRLF)

    $checked = TextBetween($examplelines, 'name="route' & $name & '" ', '></td><td>', 0, 1, 0, 1+1*$counter)
    If $checked = "checked" Then
        $checked = True
    Else
        $checked = False
    EndIf
    ConsoleWrite("Checked: " & $checked & @CRLF)


    $ip = TextBetween($examplelines, '></td><td>', '</td><td>', 0, 1+1*$counter, 0, 2+$counter*3)
    If @error then ExitLoop
    ConsoleWrite("IP: " & $ip & @CRLF)


    $mask = TextBetween($examplelines, '</td><td>', '</td><td>', 0, 2+3*$counter, 0, 2+3*$counter+1)
    If @error then ExitLoop
    ConsoleWrite("Mask: " & $mask & @CRLF)


    $gateway = TextBetween($examplelines, '</td><td>', '</td><input ', 0, 3+3*$counter, 0, 1+1*$counter)
    If @error then ExitLoop
    ConsoleWrite("GW: " & $gateway & @CRLF)
    Local $aFill[1][5] = [[$name,$checked,$ip,$mask,$gateway]]
    _ArrayAdd($a_staticroutes,$aFill)
    $counter=$counter+1
WEnd

_ArrayDisplay($a_staticroutes)

Func TextBetween($text, $starttag, $endtag, $casesensebegin = 0, $occurencebegin = 1, $casesenseend = 0, $occurenceend = -1)
    If StringInStr($text, $starttag, $casesensebegin, $occurencebegin) And StringInStr($text, $endtag, $casesenseend, $occurenceend) Then
        $begin = StringInStr($text, $starttag, $casesensebegin, $occurencebegin)
        $begin = $begin + StringLen($starttag)
        $end = StringInStr($text, $endtag, $casesenseend, $occurenceend)
        If $end > $begin Then
            $text = StringMid($text, $begin, $end - $begin)
            Return $text
        Else
            SetError(1) ;begin not > end
            Return ""
        EndIf
    Else
        SetError(2) ;tags not found
        Return ""
    EndIf
EndFunc   ;==>TextBetween

Edit: I should mention, that in some cases i will have to process hundreds of lines and performance might also be a problem with this code

Edited by Allow2010

Share this post


Link to post
Share on other sites

@Nine & @iamtheky, I am fine with the way things are and am sure we will make changes when the teams feels that's necessary. :) 

5 minutes ago, Allow2010 said:

Thanks for giving me the opportunity to give more details.

Guess my point is that you are long enough around to know that this last post should have been the initial post. Now people can have a play with it and make suggestions. ;) 

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Study this:

#include <Array.au3>

Local $examplelines = '<input type="checkbox" id="route0" name="route0" checked></td><td>1.1.1.1</td><td>2.2.2.2</td><td>3.3.3.3</td>' & _
                '<input type="checkbox" id="route1" name="route1" ></td><td>4.4.4.4</td><td>5.5.5.5</td><td>6.6.6.6</td>' & _
                '<input type="checkbox" id="route2" name="route2" checked></td><td>7.7.7.7</td><td>8.8.8.8</td><td>9.9.9.9</td>'

Local $aRes = StringRegExp($examplelines, '(?i)name="([^"]+)"\h*(\w*).*?<td>([^<]*)</td><td>([^<]*)</td><td>([^<]*)</td>', $STR_REGEXPARRAYGLOBALMATCH)
_ArrayDisplay($aRes)

Local $a_staticroutes[UBound($aRes) / 5][5]
For $i = 0 To UBound($a_staticroutes) - 1
    For $j = 0 To 4
        $a_staticroutes[$i][$j] = ($j = 1 ? ($aRes[5 * $i + $j] = "checked") : $aRes[5 * $i + $j])
    Next
Next
_ArrayDisplay($a_staticroutes)

 


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Thanks a lot, it seems to do the job nicely.

Do i understand this correct:

($j = 1 ? ($aRes[5 * $i + $j] = "checked") : $aRes[5 * $i + $j])

is it like

If $j = 1 Then
    If $aRes[5 * $i + $j] = "checked" Then
        $a_staticroutes[$i][$j] = True
    Else
        $a_staticroutes[$i][$j] = False
    EndIf
Else
    $a_staticroutes[$i][$j] = $aRes[5 * $i + $j]
EndIf

?

Share this post


Link to post
Share on other sites

a little different approach :

#include <Constants.au3>
#include <Array.au3>

Opt ("MustDeclareVars", 1)
Local $sTxt = '<input type="checkbox" id="route0" name="route0" checked></td><td>7.7.9.9</td><td>255.255.255.255</td><td>192.168.0.0</td>' & @CRLF & _
              '<input type="checkbox" id="route0" name="route0" ></td><td>7.7.9.9</td><td>255.255.255.255</td><td>192.168.0.0</td>'  & @CRLF & _
              '<input type="checkbox" id="route1" name="route1" checked></td><td>7.7.9.9</td><td>255.255.255.255</td><td>192.168.0.0</td>'  & @CRLF
Local $aRes = StringRegExp ($sTxt, '(?m)name="(.+)" (.*?)><\/td><td>(.+?)<\/td><td>(.+?)<\/td><td>(.+?)<\/td>$', $STR_REGEXPARRAYGLOBALMATCH)

Local $aFinal [0][5], $aTmp
For $i = 1 to UBound ($aRes)/5
  $aTmp = _ArrayExtract ($aRes, ($i-1)*5, $i*5-1)
  $aTmp[1] = ($aTmp[1] = "checked")
  _ArrayAdd ($aFinal, _ArrayToString($aTmp))
Next
_ArrayDisplay ($aFinal)

 

Share this post


Link to post
Share on other sites

For the fun, a variation of jchd's using StringRegExp with param 4

#include <Array.au3>

Local $examplelines = '<input type="checkbox" id="route0" name="route0" checked></td><td>1.1.1.1</td><td>2.2.2.2</td><td>3.3.3.3</td>' & _
                '<input type="checkbox" id="route1" name="route1" ></td><td>4.4.4.4</td><td>5.5.5.5</td><td>6.6.6.6</td>' & _
                '<input type="checkbox" id="route2" name="route2" checked></td><td>7.7.7.7</td><td>8.8.8.8</td><td>9.9.9.9</td>'

Local $aRes = StringRegExp($examplelines, '(?i)name="([^"]+)"\h*(\w*).*?<td>([^<]*)</td><td>([^<]*)</td><td>([^<]*)</td>', $STR_REGEXPARRAYGLOBALFULLMATCH)
;_ArrayDisplay($aRes[0])

Local $n = UBound($aRes), $a[$n][5]
For $i = 0 To $n-1
    For $j = 1 To 5
         $a[$i][$j-1] = ($j = 2 ? (($aRes[$i])[$j] = "checked") : ($aRes[$i])[$j])
    Next
Next
_ArrayDisplay($a)

 

Share this post


Link to post
Share on other sites

may have some fatal flaws, but i think it handles this specific case

#include <Array.au3>

Local $examplelines = '<input type="checkbox" id="route0" name="route0" checked></td><td>1.1.1.1</td><td>2.2.2.2</td><td>3.3.3.3</td>' & _
                '<input type="checkbox" id="route1" name="route1" ></td><td>4.4.4.4</td><td>5.5.5.5</td><td>6.6.6.6</td>' & _
                '<input type="checkbox" id="route2" name="route2" checked></td><td>7.7.7.7</td><td>8.8.8.8</td><td>9.9.9.9</td>'

local $a[0][5]

Local $aRes = StringRegExp(stringreplace($examplelines , '" >' , '" unchecked>'), '(?i)name="([^"]+)"\h*(\w*).*?<td>([^<]*)</td><td>([^<]*)</td><td>([^<]*)</td>' & "(.*?)", 3)
_ArrayAdd($a , stringreplace(stringtrimright(_ArrayToString($aRes) , 1) , "||" , @LF) , 0 , "|" , @LF , 1)
_ArrayDisplay($a)

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Thanks to all who worked on this problem. I am still struggling to completely understand with the regexp parts, but it is getting clearer 🙂

Share this post


Link to post
Share on other sites
On 7/10/2019 at 10:26 AM, Allow2010 said:

Do i understand this correct

Exactly.

Wait, I strongly suspect something even more involved is in the pipe...

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Credits to jchd for having enlighten my knowledge on this one  :D   

#Include <Array.au3>

Local $examplelines = '<input type="checkbox" id="route0" name="route0" checked></td><td>1.1.1.1</td><td>2.2.2.2</td><td>3.3.3.3</td>' & @crlf & _
                '<input type="checkbox" id="route1" name="route1" ></td><td>4.4.4.4</td><td>5.5.5.5</td><td>6.6.6.6</td>' & @crlf & _
                '<input type="checkbox" id="route2" name="route2" checked></td><td>7.7.7.7</td><td>8.8.8.8</td><td>9.9.9.9</td>'

Local $s = Execute(StringRegExpReplace($examplelines, _
    '(?m)^.*name="([^"]+)"\h*(\w*).*?<td>([^<]*)</td><td>([^<]*)</td><td>([^<]*)</td>(\R?)', _
    "''" & " & '$1|' & " & "('$2'='checked') & " & "'|$3|$4|$5' & '$6' & ") & "''")
Local $a[0][5]
_ArrayAdd($a, $s)
_ArrayDisplay($a)

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...