Parse ajax request that returns a page to xml
I want to build an web mobile app, and i want to get data from a url, the problem is that url returns a whole webpage.. let me explain better: this url for example: http://tubeplus.me/search/movies/war/0/
the "war" in the url is the keyword i used in a search in the site that returned to me some chart and a list of movies, i want to have only this list of movies returned, in short i wan开发者_运维技巧t to wipe out all html then make a "loop" while i write down only the titles returned. its like converting it to xml so i can parse in a different way.... how can i accomplish this? Thanks
That depends how reliable the structure of the HTML is. You can load it up and select like normal DOM if you know what to select. You mention this as a mobile app, I would highly recommend doing the getting and parsing on your server, and serve up the list you want, rather than having the user do it on their phone, which has limited connection speed and limited power.
If the page returns valid XHTML, you could load this into an xml object and select that way as well. If it's not, then loading it with some DOM simulating library is best.
jQuery Code:
var jCollectionMovieDIVs = $(html).find('div.movie');
... but like I said, better to do it on the server than have the phone do it.
What you want to do is basically screen scraping and take out the necessary info. This method is unstable if you don't own the page as the structure of the HTML could change from time to time. But, if you think you could not get the API version that returning the result, here are your options:
- Parse it on the server In this scenario, you create a middle tier that is basically getting the page and do screen scraping here. If the HTML is organized enough (which it does seem like it for that page in your example), then you could just walk through the HTML tree using HTMLParser and get the elements that you want and change the format. For PHP, try using HTML Parser such as PHP Simple HTML. You could get the HTML and manipulate it so you could change it into JSON/XML format that fits better with your AJAX on mobile frontend
- Parsing it on your phone In this scenario, you would use your native code phone API (iOS/Android/Blackberry) to do the same thing as above
- Parse it via AJAX in Javascript Similar to above but highly unrecommended as you are already wrapped in the WebBrowser and Phone with limited power
From the structure of the page in your example, it seems that all you need is getting the div with the id "list_body", from there you get the children which contains the info that you want
精彩评论