N3xT Posted September 25, 2016 Share Posted September 25, 2016 Hello guys, hope u are fine today I have a little problem, how I can get a text from website to Lua, That's all Link to comment
L3KZ!K4 Posted September 25, 2016 Share Posted September 25, 2016 fetchRemote Due the fact this an website, you'll need to filter the information you want. Of course to get a text remotely you should use an API, to get only the information you need. Link to comment
N3xT Posted September 25, 2016 Author Share Posted September 25, 2016 6 hours ago, L3KZ!K4 said: fetchRemote Due the fact this an website, you'll need to filter the information you want. Of course to get a text remotely you should use an API, to get only the information you need. I don't know a lot about websites so, How I can filter the results? can I get the text without API?. Link to comment
Mr.Loki Posted September 26, 2016 Share Posted September 26, 2016 12 hours ago, N3xT said: Hello guys, hope u are fine today I have a little problem, how I can get a text from website to Lua, That's all what site and what text on the site are you trying to get? Link to comment
N3xT Posted September 26, 2016 Author Share Posted September 26, 2016 1 minute ago, loki2143 said: what site and what text on the site are you trying to get? Private site. Link to comment
Mr.Loki Posted September 26, 2016 Share Posted September 26, 2016 Just now, N3xT said: Private site. ah ok, well i can't help ya there lol Link to comment
Dealman Posted September 26, 2016 Share Posted September 26, 2016 Well, if it's using XHTML you can use the regular XML functions to parse through it and find what you need. Using PHP should work for you as well but I haven't really touched any PHP yet so I'm rather inexperienced with that. Link to comment
N3xT Posted September 26, 2016 Author Share Posted September 26, 2016 30 minutes ago, Dealman said: Well, if it's using XHTML you can use the regular XML functions to parse through it and find what you need. Using PHP should work for you as well but I haven't really touched any PHP yet so I'm rather inexperienced with that. Yes, I'm using XHTML, but my question how can I do it? I know about " regular XML functions ", but how to search for it after connect? Link to comment
Dealman Posted September 26, 2016 Share Posted September 26, 2016 Use fetchRemote to save it as text, you then pass this onto the client to read through it using the XML functions(unless you need to do it on the server). To see how things are structured, right-click your own page and save as. I was working on a resource before which would fetch internet radios from internet-radio.com and parse through them for easy streaming. Here's a snippet of the code to see how I parsed through it; for nodeIndex, theNode in ipairs(stationNodes) do if(xmlNodeGetName(theNode) == "tr") then if(nodeIndex <= #stationNodes-1) then local stationInfo = xmlNodeGetChildren(theNode); for nodeIndex, theNode in ipairs(stationInfo) do if(xmlNodeGetName(theNode) == "td") then if(nodeIndex == 1) then -- Parse URL local urlNode = xmlFindChild(theNode, "a", 0); local onclickAttribute = xmlNodeGetAttribute(urlNode, "onclick") local findURL = string.match(onclickAttribute, "http?://[%w-_%.%?%.:/%+=&]+"); --outputChatBox("Debug URL: ["..tostring(findURL).."]"); table.insert(g_RadioStations_URL, #g_RadioStations_URL, findURL); elseif(nodeIndex == 2) then -- Parse Name local nameNode = (xmlFindChild(theNode, "span", 0) or xmlFindChild(theNode, "a", 0)); if(theName ~= false) then local theName = xmlNodeGetValue(nameNode); local theNameFix = string.gsub(theName, "[\n+]", ""); local theNameFix = string.gsub(theName, "&", ""); --outputChatBox("Debug Name: ["..tostring(theNameFix).."]"); table.insert(g_RadioStations_Name, #g_RadioStations_Name, theNameFix); end elseif(nodeIndex == 3) then -- Parse Bitrate local bitRateRaw = xmlNodeGetValue(theNode); local bitRateFix = string.gsub(bitRateRaw, "[\n+]", ""); --outputChatBox("Debug Bitrate: ["..tostring(bitRateFix).."]"); table.insert(g_RadioStations_Bitrate, #g_RadioStations_Bitrate, bitRateFix); end end end end end end 1 Link to comment
N3xT Posted September 27, 2016 Author Share Posted September 27, 2016 Nothing happen. <body> <td class="sourceName">Tn6eL</td> </body> addCommandHandler("getSourceName", function (p) local fileName = "sourceCode.xml" local xmlFile = xmlLoadFile(fileName) if xmlFile then local songName = xmlNodeGetChildren(xmlFile); for i,node in ipairs(songName) do local nameNode = xmlFindChild(node, "sourceName", 0)) outputChatBox(xmlNodeGetValue(nameNode),p) end xmlUnloadFile(xmlFile) end end ) Link to comment
Dealman Posted September 27, 2016 Share Posted September 27, 2016 (edited) You should read up on XML a bit, you're confusing the child, attribute and value. In this particular scenario;<td class="sourceName">Tn6e</td> Red is the child, green is an attribute and blue is the value. So in order to get the value, you'd have to use this code; addCommandHandler("getSourceName", function (p) local fileName = "sourceCode.xml" local xmlFile = xmlLoadFile(fileName) if xmlFile then local findNode = xmlFindChild(xmlFile, "td", 0) if(findNode) then outputChatBox(tostring(xmlNodeGetValue(findNode, "class")), p) end xmlUnloadFile(xmlFile) end end) Edited September 27, 2016 by Dealman Link to comment
N3xT Posted September 27, 2016 Author Share Posted September 27, 2016 1 hour ago, Dealman said: You should read up on XML a bit, you're confusing the child, attribute and value. In this particular scenario;<td class="sourceName">Tn6e</td> Red is the child, green is an attribute and blue is the value. So in order to get the value, you'd have to use this code; addCommandHandler("getSourceName", function (p) local fileName = "sourceCode.xml" local xmlFile = xmlLoadFile(fileName) if xmlFile then local findNode = xmlFindChild(xmlFile, "td", 0) if(findNode) then outputChatBox(tostring(xmlNodeGetValue(findNode, "class")), p) end xmlUnloadFile(xmlFile) end end) Doesn't work, can't find child Link to comment
Dealman Posted September 27, 2016 Share Posted September 27, 2016 (edited) It should work if your XML file looks like you showed above; <body> <td class="sourceName">Tn6eL</td> </body> If not, the error is elsewhere in your code - use debugscript. Edited September 27, 2016 by Dealman Link to comment
N3xT Posted September 27, 2016 Author Share Posted September 27, 2016 4 minutes ago, Dealman said: It should work if your XML file looks like you showed above; <body> <td class="sourceName">Tn6eL</td> </body> If not, the error is elsewhere in your code - use debugscript. No errors, my XML file have a lot of lines else that I reply, maybe that is the problem? Link to comment
Dealman Posted September 27, 2016 Share Posted September 27, 2016 Yes, use xmlNodeGetChildren to loop through them. Use xmlNodeGetAttribute to check if the attribute equals "sourceName". If true, use xmlNodeGetValue to get the value. Link to comment
N3xT Posted September 27, 2016 Author Share Posted September 27, 2016 9 minutes ago, Dealman said: Yes, use xmlNodeGetChildren to loop through them. Use xmlNodeGetAttribute to check if the attribute equals "sourceName". If true, use xmlNodeGetValue to get the value. addCommandHandler("getSourceName", function (p) local fileName = "sourceCode.xml" local xmlFile = xmlLoadFile(fileName) if xmlFile then local songName = xmlNodeGetChildren(xmlFile) for i,node in ipairs(songName) do if ( xmlNodeGetName(node) == "td" ) then local nodeName = xmlNodeGetAttribute(node, "sourceName") if nodeName == false then outputChatBox(xmlNodeGetValue(nodeName),p) end end end xmlUnloadFile(xmlFile) end end ) Doesn't work again xd. Link to comment
Dealman Posted September 27, 2016 Share Posted September 27, 2016 Kind of hard to help you when I don't know how the file's structured. You'll just have to do a bit of trial and error, use outputChatBox to debug, see what values you are getting - use that to try and pin-point where in the file you are. Remember that children can have children and so on, XML is like navigating in a spider-web or finding a needle in a haystack sometimes 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now