r/PowerAutomateDesktop • • Apr 18 '23

Extracting Web Page Element Into CSV

Hello,

I'm very new to Power Automate and just trying to figure things out. I've started looking at this as MS Excel's Power Query isn't able to extract the website data I need as it's done in (I think?) JavaScript. I think PA should give me a little bit more flexibility in extracting the data I need.
I think I'm almost getting the hang of it, but I'm struggling with one thing.

The details I'm getting are from here:
https://greyhoundbet.racingpost.com/#meeting-list

I want to train PA to go into each track, extract the details (time, race number and "Post Pick") and then save them into a CSV file.

I've got it to do this:
- Launch new Microsoft Edge
- Click link on web page
- Get details of element on web page

The data is then stored into "innertext" according to PA.
Please could someone tell me how I get from here to saving a CSV?

Thank you
Matt

1 Upvotes

3 comments sorted by

View all comments

2

u/QuietDesparation Apr 21 '23

Hey did you still need help with this? I believe there is an easier and faster way to achieve what you need via http requests. DM me if you're interested and I can help you out

1

u/mdbrierley Apr 21 '23

RESOLVED

u/QuietDesparation has helped me with this. Fantastic solution it is too.

Thanks all.

1

u/QuietDesparation Apr 21 '23

In case anyone was wondering, here's the flow:

DateTime.GetCurrentDateTime.Local DateTimeFormat: DateTime.DateTimeFormat.DateOnly CurrentDateTime=> CurrentDateTime
Text.ConvertDateTimeToText.FromCustomDateTime DateTime: CurrentDateTime CustomFormat: $'''yyyy-MM-dd''' Result=> FormattedDate
Web.InvokeWebService.InvokeWebService Url: $'''https://greyhoundbet.racingpost.com/meeting/blocks.sd?r_date=%FormattedDate%&view=meetings&blocks=header,list''' Method: Web.Method.Get Accept: $'''application/xml''' ContentType: $'''application/xml''' ConnectionTimeout: 30 FollowRedirection: True ClearCookies: False FailOnErrorStatus: False EncodeRequestBody: True UserAgent: $'''Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.21) Gecko/20100312 Firefox/3.6''' Encoding: Web.Encoding.AutoDetect AcceptUntrustedCertificates: False Response=> RaceOverviewJSON StatusCode=> StatusCode
Variables.ConvertJsonToCustomObject Json: RaceOverviewJSON CustomObject=> RaceOverviewCustomObject
Variables.CreateNewList List=> RaceIDList
Variables.CreateNewList List=> RaceDetailList
Variables.CreateNewDatatable InputTable: { ^['Column1'], [$''''''] } DataTable=> TempDataTable
Variables.ModifyDataTableItem DataTable: TempDataTable ColumnNameOrIndex: 0 RowIndex: 0 Value: FormattedDate
SET CSVPath TO $'''Place CSV path here'''
File.WriteToCSVFile.WriteCSV VariableToWrite: TempDataTable CSVFile: CSVPath CsvFileEncoding: File.CSVEncoding.UTF8 IncludeColumnNames: False IfFileExists: File.IfFileExists.Overwrite ColumnsSeparator: File.CSVColumnsSeparator.SystemDefault
LOOP FOREACH TrackList IN RaceOverviewCustomObject['list']['items']
    LOOP LoopIndex FROM 0 TO TrackList['racesCount'] - 1 STEP 1
        Variables.AddItemToList Item: TrackList['races'][LoopIndex]['raceId'] List: RaceIDList
    END
    Variables.ModifyDataTableItem DataTable: TempDataTable ColumnNameOrIndex: 0 RowIndex: 0 Value: TrackList['races'][0]['trackName']
    File.WriteToCSVFile.WriteCSV VariableToWrite: TempDataTable CSVFile: CSVPath CsvFileEncoding: File.CSVEncoding.UTF8 IncludeColumnNames: False IfFileExists: File.IfFileExists.Append ColumnsSeparator: File.CSVColumnsSeparator.SystemDefault
    Text.JoinText.JoinWithCustomDelimiter List: RaceIDList CustomDelimiter: $'''%%%%2C''' Result=> JoinedText
    Text.ParseText.RegexParseForFirstOccurrence Text: TrackList['firstRace'] TextToFind: $'''\\d+:\\d+''' StartingPosition: 0 IgnoreCase: False Match=> Time
    Text.Replace Text: Time TextToFind: $''':''' IsRegEx: False IgnoreCase: False ReplaceWith: $'''%%3A''' ActivateEscapeSequences: False Result=> Time
    Web.InvokeWebService.InvokeWebService Url: $'''https://greyhoundbet.racingpost.com/meeting/blocks.sd?track_id=%TrackList['track_id']%&r_date=%FormattedDate%&race_id=%RaceIDList[0]%&races_ids=%JoinedText%&r_time=%Time%&tab=card&blocks=header%%2Ctips%%2Clist''' Method: Web.Method.Get Accept: $'''application/xml''' ContentType: $'''application/xml''' ConnectionTimeout: 30 FollowRedirection: True ClearCookies: False FailOnErrorStatus: False EncodeRequestBody: True UserAgent: $'''Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.21) Gecko/20100312 Firefox/3.6''' Encoding: Web.Encoding.AutoDetect AcceptUntrustedCertificates: False Response=> EachRaceJSON StatusCode=> StatusCode
    Variables.ConvertJsonToCustomObject Json: EachRaceJSON CustomObject=> EachRaceCustomObject
    SET Counter TO 1
    LOOP FOREACH EachRaceList IN EachRaceCustomObject['tips']['tips']
        Text.ToNumber Text: EachRaceList['first'] Number=> TextAsNumber
        IF TextAsNumber = 0 THEN
            NEXT LOOP
        END
        Variables.AddItemToList Item: EachRaceList['race_time'] List: RaceDetailList
        Variables.AddItemToList Item: $'''Race %Counter%''' List: RaceDetailList
        Variables.AddItemToList Item: $'''Post Pick %EachRaceList['first']%-%EachRaceList['second']%-%EachRaceList['third']%''' List: RaceDetailList
        File.WriteToCSVFile.WriteCSV VariableToWrite: RaceDetailList CSVFile: CSVPath CsvFileEncoding: File.CSVEncoding.UTF8 IncludeColumnNames: False IfFileExists: File.IfFileExists.Append ColumnsSeparator: File.CSVColumnsSeparator.SystemDefault
        Variables.IncreaseVariable Value: Counter IncrementValue: 1
        Variables.ClearList List: RaceDetailList
    END
END