Display cell table data in a TextView using Jsoup - android

I want to display in a TextView the Snow in the past 24 hours of a ski resort. I used the CSS path and tried other ways but nothing happens the TextView doesn't display nothing.
The web page: http://www.arizonasnowbowl.com/resort/snow_report.php
The CSS path: #container > div.right > table.interior > tbody > tr:nth-child(2) > td.infoalt
private class Description extends AsyncTask<Void, Void, Void> {
String desc;
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(Snowreport.this);
mProgressDialog.setTitle("Snow Report");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect(url).get();
Elements elms = document.select("td.infoalt");
for(Element e:elms)
if(e.className().trim().equals("infoalt"))
//^^^<--trim is required as,
// their can be leading and trailing space
{
TextView txtdesc = (TextView) findViewById(R.id.snowp24);
txtdesc.setText((CharSequence) e);
}
mProgressDialog.dismiss();
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}

The code:
Element div = doc.getElementById("contentinterior");
Elements tables = div.getElementsByTag("table");
Element table = tables.get(1);
String mSnow = table.getElementsByTag("tr").get(1).getElementsByTag("td").get(1).text();

You may have the incorrect String for the selection parameter. The correct selection to use as a parameter for Document.select() can be found by 'Inspecting the element' of a webpage most easily done by right clicking in the Chrome browser.
The following code may produce a better result for you:
final Elements tableElements = response.parse()
.getElementsByClass("info")
.select("td");
for (Element element : tableElements) {
String string = element.getElementsByClass("infoalt").text().trim()
Log.d("Jsoup", string);
}
Good luck and happy coding!

Related

Scraping google search first page using Jsoup with AsyncTask fails?

I've been using Jsoup in order to fetch certain words from google search but it fails to my understanding in the Jsoup query process.
It's getting successfully into the doInBackground method but it won't print the title and body of each link on the search.
My guess is that the list I'm getting from doc.select (links) is empty.
which brings it to query syntax problem
value - it's the keyword search, in my case, it's a barcode that actually works. Here's the link
Here it's the async call from another class:
String url = "https://www.google.com/search?q=";
if (!value.isEmpty())
{
url = url + value + " price" + "&num10";
Scrape_Asynctasks task = new Scrape_Asynctasks();
task.execute(url);
}
and here is the async task itself:
public class Scrape_Asynctasks extends AsyncTask<String, Integer, String>
{
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(String... strings) {
try
{
Log.i("IN", "ASYNC");
final Document doc = Jsoup
.connect(strings[0])
.userAgent("Jsoup client")
.timeout(5000).get();
Elements links = doc.select("li[class=g]");
for (Element link : links)
{
Elements titles = link.select("h3[class=r]");
String title = titles.text();
Elements bodies = link.select("span[class=st]");
String body = bodies.text();
Log.i("Title: ", title + "\n");
Log.i("Body: ", body);
}
}
catch (IOException e)
{
Log.i("ERROR", "ASYNC");
}
return "finished";
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
Don't use "Jsoup client" as your user agent string. Use the same string as your browser, eg. "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0". Some sites (including google) don't like it.
Your first selector should be .g: Elements links = doc.select(".g");
The sites uses javascript, so you will not get all the results as you get in your browser.
You can disable JS in your browser and see the difference.

How to get innerHTML from webview in android

Hi i have searched the internet and stackoverflow all day without any solution! :(
My problem is:
I have to show some schedules in a app, and the data is stored on a webpage, normally on PC you visit the page, enter your id, and it shows the schedule...
but how do i get to the schedule without interacting with a webview or stuff like that? i have to save some specific html data after login...
i have tired with jsoup, but after login, then the url changes, and i dont know how to get it, therefore i tried with webview, but this didnt work either
please help :)
public class getHTML extends AsyncTask{
String words;
#Override
protected Void doInBackground(Void... params) {
try {
final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
final String FORM_URL = "http://timetable.scitech.au.dk/apps/skema/VaelgElevskema.asp?webnavn=skema";
final String SCHEDULE_URL = "http://timetable.scitech.au.dk/apps/skema/ElevSkema.asp";
final String USERID = "201506426";
// # Go to search page
Connection.Response loginFormResponse = Jsoup.connect(FORM_URL)
.method(Connection.Method.GET)
.userAgent(USER_AGENT)
.execute();
// # Fill the search form
FormElement loginForm = (FormElement)loginFormResponse.parse()
.select("form").first();
// ## ... then "type" the id ...
Element loginField = loginForm.select("input[name=aarskort]").first();
loginField.val(USERID);
// # Now send the form
Connection.Response loginActionResponse = loginForm.submit()
.cookies(loginFormResponse.cookies())
.userAgent(USER_AGENT)
.execute();
// # go to the schedule
Connection.Response someResponse = Jsoup.connect(SCHEDULE_URL)
.method(Connection.Method.GET)
.userAgent(USER_AGENT)
.execute();
// # print out the body
Element el = someResponse.parse()
.select("body").first();
words = el.text();
System.out.println(loginActionResponse.parse().html());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
tv.setText(words);
Toast.makeText(MainActivity.this, "DET VIRKER!", Toast.LENGTH_SHORT).show();
}
}

how to get an image from HTML page Android studio

I have an image in JPG in a sit (I suppose it is HTML format but I am not sure about it). I open the source of the page and I see there the image I need written this way.
If I take the link it show me the image.
But i don't know how can I get from the URL page to get this link. It is not look like written in JSON format.
How can I get it?
Thanks
Bar.
After some play I get to this:
The meta is the elements, and og.image and content are one of there meta data attribute.
So I do as follow to get the image URL string
String imageLink=null;
try {
Log.d(TAG, "Connecting to [" + strings[0] + "]");
Document doc = Jsoup.connect(strings[0]).get(); // put all the HTML page in Document
// Get meta info
Elements metaElems = doc.select("meta");
for (Element metaElem : metaElems) {
String property = metaElem.attr("property");
if(property.equals("og:image"))// if find the line with the image
{
imageLink = metaElem.attr("content");
Log.d(TAG, "Image URL" + imageLink );
}
}
} catch (Exception e) {
e.printStackTrace();
exception =e;
return null;
}
Here I am posting the small code snippet for ingrate this kind of functionality may this help you.
Step 1: Add below gradle
compile 'org.jsoup:jsoup:1.10.2'
Step 2:
Use below async task for get all meta information from any Url.
public class MainActivity extends AppCompatActivity {
private ImageView imgOgImage;
private TextView text;
String URL = "https://www.youtube.com/watch?v=ufaK_Hd6BpI";
String UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
imgOgImage = (ImageView) findViewById(R.id.imgOgImage);
new FetchMetadataFromURL().execute();
}
private class FetchMetadataFromURL extends AsyncTask<Void, Void, Void> {
String websiteTitle, websiteDescription, imgurl;
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params) {
try {
// Connect to website
Document document = Jsoup.connect(URL).get();
// Get the html document title
websiteTitle = document.title();
//Here It's just print whole property of URL
Elements metaElems = document.select("meta");
for (Element metaElem : metaElems) {
String property = metaElem.attr("property");
Log.e("Property", "Property =" + property + " \n Value =" + metaElem.attr("content"));
}
// Locate the content attribute
websiteDescription = metaElems.attr("content");
String ogImage = null;
Elements metaOgImage = document.select("meta[property=og:image]");
if (metaOgImage != null) {
imgurl = metaOgImage.first().attr("content");
System.out.println("src :<<<------>>> " + ogImage);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
text.setText("Title : " + websiteTitle + "\n\nImage Url :: " + imgurl);
//t2.setText(websiteDescription);
Picasso.with(getApplicationContext()).load(imgurl).into(imgOgImage);
}
}
}
Note : Here I have just roughly making this demo.no any coding standard will user so please take care this while you ingrate this code in your application.I am just making this demo for learning purpose only.
Here I am just used youtube url for display meta data.you can used any url based on your requirement.
I hope you are clear with my logic.
Good Luck

How to get "title" with Jsoup html parser?

I have code,which can extract text value of the class. But in this class, exist attribute "title", which I should get. While I can extract only class ".day__description". Below part my code.
try {
Document document = Jsoup.connect(URL).get();
Elements nodeDay = document.select(".day__description");
if(nodeDay.size() > 0) {
day = nodeDay.get(0).text();
}
} catch (IOException e) {
day = "Not found";
}
return null;
protected void onPostExecute(Void result) {
TextView txttitle = (TextView) findViewById(R.id.tv);
txttitle.setText(day);
}
I need to get "title" from this part of the site:
<div class="day__description" title="SomeText">...</div>
As a result in variable "day", should be text "Some Text". Sorry for my English))
I hope all understand. Thanks for advance.
Try:
Document doc = Jsoup.connect(url).get();
Elements div=doc.select("div.day__description");
System.out.println(div.attr("title"));

How to show images and text with listview from web site

I use Jsoup to parse HTML page and I want to show text and images in the ListView. So I created LinkedHashMap and SimpleAdapter for this. Besides text shows as is. Images don't wan't to be shown. Every time I get log message such as: "resolve Uri failed on bad bitmap uri". I tried to google this problem but still can't resolve it.
Here is the code:
#Override
protected String doInBackground(String... arg) {
Document doc;
try {
doc = Jsoup.connect("http://thesiteiuse.com/news/").get();
title = doc.select("h2[class=et_pt_title]");
picture = doc.select("img");
listViewContent.clear();
for (Element titles : title) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put(ATTRIBUTE_NAME_TEXT, titles.text());
listViewContent.add(map);
}
for (Element img : picture){
Bitmap bitmap;
Map<String,Object> picMap = new LinkedHashMap<String,Object>();
String iurl;
iurl = img.absUrl("src");
Log.w("ABSurl:", iurl.toString());
URL url = new URL(iurl);
bitmap = BitmapFactory.decodeStream(url.openStream());
Log.w("BITMAP",bitmap.toString());
picMap.put(ATTRIBUTE_NAME_IMAGE, String.valueOf(bitmap));
listViewContent.add(picMap);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
How can I solve my problem? Maybe is there another simple way to display images in the ListView which app get from the URL?
You have to create your own adapter to the listview. You should see this tutorial on how to create it: http://www.vogella.com/tutorials/AndroidListView/article.html

Categories

Resources