Get data from http

#1

Hi,
I have got a question about splitting http request.
I want to get one part of a string from a website. Therefore I am using http request and then I would like to split it.

After I split it, I get two strings in the payload. Now I want to work with only one of them. I thing it is very easy, but I don't get it.

Fyi: It is the printable version of myfittnespal diary and I want to get the total carbs number of the day.For that reason I just made a test account and this is my node so far:

[{"id":"24b0737d.a3ff5c","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"5d69ec65.08d684","type":"inject","z":"24b0737d.a3ff5c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":60,"wires":[["ed0452de.b2a24"]]},{"id":"ed0452de.b2a24","type":"http request","z":"24b0737d.a3ff5c","name":"","method":"GET","ret":"txt","url":"https://www.myfitnesspal.com/reports/printable_diary/testaccount111112222","tls":"","x":169,"y":121,"wires":[["7435ed21.ba7444"]]},{"id":"3e34057f.c092ca","type":"debug","z":"24b0737d.a3ff5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":631,"y":116,"wires":[]},{"id":"3a6de1a.f00db1e","type":"split","z":"24b0737d.a3ff5c","name":"","splt":"TOTAL","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":491,"y":120,"wires":[["3e34057f.c092ca"]]},{"id":"7435ed21.ba7444","type":"change","z":"24b0737d.a3ff5c","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"diabetes","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":346,"y":118,"wires":[["3a6de1a.f00db1e"]]}]

Thanks for your help

0 Likes

#2

Hi @ulfdogg

as the content you are getting is HTML, you can use the HTML node to extract different parts of the page using CSS selectors.

Looking at the page, the total carbs info is inside a <td> element, inside a <tfoot>, but there's not way to narrow it down to just the one field. So we can configure the HTML node to select tfoot td - that will give back an array of all of the total values. The total carbs is the 3rd array element, so a Change node can then pull that out:

[{"id":"8dd9347e.e666e8","type":"inject","z":"43c15ecc.a8127","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":60,"wires":[["71f9cf7.b08123"]]},{"id":"71f9cf7.b08123","type":"http request","z":"43c15ecc.a8127","name":"","method":"GET","ret":"txt","url":"https://www.myfitnesspal.com/reports/printable_diary/testaccount111112222","tls":"","x":169,"y":121,"wires":[["3f3136cc.2f1aea"]]},{"id":"d741414c.018f4","type":"debug","z":"43c15ecc.a8127","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":710,"y":120,"wires":[]},{"id":"3f3136cc.2f1aea","type":"html","z":"43c15ecc.a8127","name":"","property":"payload","outproperty":"payload","tag":"tfoot td","ret":"html","as":"single","x":340,"y":120,"wires":[["8e6a974.d7d3d68"]]},{"id":"8e6a974.d7d3d68","type":"change","z":"43c15ecc.a8127","name":"Get Total Carbs","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[2]","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":120,"wires":[["d741414c.018f4"]]}]
0 Likes

#3

Try to extract data from HTML using CSS selector and HTML node? (ah, @knolleary answered as I was typing it.. I'll post it anyway)

[{"id":"c4587504.8a7e68","type":"inject","z":"e9bac780.ed1af8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":160,"y":60,"wires":[["41070ad2.6f8a4c"]]},{"id":"41070ad2.6f8a4c","type":"http request","z":"e9bac780.ed1af8","name":"","method":"GET","ret":"txt","url":"https://www.myfitnesspal.com/reports/printable_diary/testaccount111112222","tls":"","x":350,"y":60,"wires":[["e447b569.609a8"]]},{"id":"e447b569.609a8","type":"html","z":"e9bac780.ed1af8","name":"extract data from HTML","tag":"#food > tbody > tr:nth-child(2) > td:nth-child(3)","ret":"html","as":"single","x":570,"y":60,"wires":[["23500917.530276"]]},{"id":"23500917.530276","type":"debug","z":"e9bac780.ed1af8","name":"","active":true,"console":"false","complete":"false","x":790,"y":60,"wires":[]}]
1 Like

#4

@Marooned much better - I always go blank on the nth-child syntax.

0 Likes

#5

Thank you very much

0 Likes