Android JSoup Example - android

I was just wondering has anyone got a sample eclipse project with a working implementation of JSoup? Im trying to use it to pull information from websites and have gone all over google trying to get it to work but cant. If anyone could help I'd really appreciate it.

JSoup is really easy to use, look at these exemples from the JSoup cookbook:here
First, You have to connect to the webpage you want to parse using:
Document doc = Jsoup.connect("http://example.com/").get();
Then, you can select page elements using the JSoup selector syntax.
For instance, say you want to select all the content of the div tags with the id attribute set to test, you just have to use:
Elements divs = doc.select("div#test");
to retrieve the divs, then you can iterate on them using:
for (Element div : divs)
System.out.println(div.text());
}

Related

How to get text point of Element or Node in jsoup

I'm using Jsoup and need to know text point of Element or Node in jsoup. Example: I have html: <p><span>1</span></p> then I need to know text point of <p> is 0,<span> is 4,</span> is 10... How to do that?
Currently you can't do this in Jsoup, for it does not keep track of the positions of tags in the original input. There was some discussion going on about this earlier (JSOUP HTML Parser)
The solution is to use another parser that explicitly supports this feature. The other post suggested Jericho.

Android associate html tag to xml style

I am working on a cross platform application for a school for my internship, teachers can write courses and everything is stored in html in a database.
I am doing the Android application and will perform a request to the API we created, so now I am getting an html document, is there any good practices about converting title to some xml predefined style ?
You can try with http://jsoup.org/
Also you can use myTextView.setText( Html.fromHtml("<h2>Your html string</h2>") );

JSoup Screen Scraping With Many Divs

I have a page I want to scrape with android, and the contents are want are located like this:
body
div#wrapper
div#mainContentArea
div#scheduleModule
div#scheduleDayView
div#scheduleDayViewScroll
div#scheduleItemContainer
div#eventContainer
div#SSPP_o090570*A*
div.eventInfo
p.eventText
span.eventInfoDefault
How can I access the span using jsoup?
If you don't want to be taken out in the streets and whipped for your transgressions, you will split up that block of text there.
Anyway, you want to find the span whose class is eventInfoDefault? Well:
Document site = Jsoup.connect("http://www.example.com");
Element span = site.select("span.eventInfoDefault").first();
//Proceed to do whatever you want with that below.
Source: http://jsoup.org/cookbook/extracting-data/selector-syntax

Extracting data using JSoup

I am trying to extract product name information from Google Shopping (http://www.google.co.uk/m/products?q=5010459007289, phone website).
The product name always appear in between the span with class "owb63p",for example
"<span class="owb63p">Highland Spring Sports Bottle 750 Ml</span>"
I am new with JSoup, I can connect with the URL and get the whole document, but I just need help setting it up so that I only get the piece of information I need.
In JSoup it will be like:
Document doc = Jsoup.connect("www.google.co.uk/m/products?q=5010459007289").get();
Element title = doc.select("span.owb63p").first();
System.out.println(title.text());
I don't like JSoup that much, but with apache jericho it would like :
Source source=new Source(new URL(sourceUrlString));
String content=source.getFirstElementByClass( "owb63p" ).getContent().toString();
It looks like JSoup examples has what you are looking for.
You could try
doc.select("span").get(0).data();
or you can simply iterate for multiple span tags...

How to get a specific tag from a div class with this html page?

I am trying to retrieve the image url in from this html page
The image is inside of the editions box on the webpage. How would i go about getting it using the JSoup selector method.
Such as
Document doc = Jsoup.connect(url).get();
Element png = doc.select(//What would the tag be?);
I have an idea of how to set it up, just not how to retrieve the tag.
doc.select("div.box-art").select("img").attr("abs:src"));
From the docs it looks like doc.select(".box-art img") should do the trick. (Select an img element which is the child of an element of class box-art.) Note that this could get you multiple imgs, (if JSoup supports that).

Categories

Resources