Submitting a search in website with Jsoup - android

I want to create an app that allows me to search for a city in this weather site.
I would like you to return the site corresponding to the search performed.
I tried to do this with an EditText and a Button, but the search does not seem to work, because the page returned is the same as the initial one.
How can I solve this problem?
This is my code:
final EditText editText = findViewById(R.id.edit);
final TextView textView = findViewById(R.id.testo);
Button button = findViewById(R.id.clicca);
button.setOnClickListener(new View.OnClickListener() {
Connection.Response res = null;
Document doc;
#Override
public void onClick(View v) {
try {
doc = Jsoup.connect("https://www.ilmeteo.it/meteo/cerca")
.data("citta", "bari")
.post();
} catch (IOException e) {
e.printStackTrace();
}
textView.setText(doc.location());
}
});
This is the site's HTML code:
<div id="search">
<a id="search-logo" href="https://www.ilmeteo.it" title="IL Meteo - Home Page"></a>
<a id="search-arrow" href="javascript:;" onclick="toggleSearchMenu('main');"></a>
<form id="form-search0" name="search0" action="https://www.ilmeteo.it/meteo/cerca" method="get" onsubmit="return CheckSearchForm0()">
<input id="search-main" name="citta" value="" size="17" maxlength="64" class="txtSearch" onfocus="this.className='txtSearch';openSearchMenu('main');virginSearch=false;" onblur="if(this.value=='')this.className='txtSearch txtSearchE'" title="Cerca comune o località" autocomplete="Off" tabindex="1" onkeyup="ajax_showOptions(this,'type=IT&sort=smart',event)" type="text">
</form>
<a id="search-button" href="javascript:;" onclick="$('#form-search0').submit()"></a>
<div id="fav-search-cont"><span id="fav-search"></span></div>
</div>
EDIT
Thank you all! Your answers have solved my problem :)
But I have problems with another weather site. How do I instead from this other site do the same operation as before?
P.S .: the problem of this site is the mandatory click of the city to search and that next to the city in the URL there is a code, like this "http://www.meteo.it/meteo/roma-58091".
This is the second site's HTML code:
<div class="pksrc">
<form class="search-form" onsubmit="return false">
<fieldset class="icon-lens">
<input type="hidden" id="searchid" disabled="" value="">
<input type="hidden" id="searchtarget" value="_blank">
<input type="text" class="query " id="searchinput" name="search" value="" placeholder="Cerca località" autocomplete="off">
<input type="submit" value="submit">
</fieldset>
</form>
<div id="search-menu"></div>
<ul id="search-option">
<li>Milano</li>
<li>Roma</li>
<li>Napoli</li>
</ul>
</div>

Answer for your second question:
This is the way I found out to navigate to the "city" page you want to search.
Step 1:
Pass the starting letter of the city to the request and get a JSON response.
Ex: If you want to search "Milano", then get the results for letter "m" using this URL http://www.meteo.it/autosuggest/m.json?
The sample JSON response is:
{
"url": [
{
"ita": "meteo",
"sea": "meteo-mare",
"ski": "meteo-montagna",
"eur": "meteo",
"wor": "meteo"
}
],
"results": [
{
"code": "15146",
"value": "Milano (MI)",
"value_it": "milano",
"value_en": "milan",
"url": "ita"
},
{
"code": "20030",
"value": "Mantova (MN)",
"value_it": "mantova",
"value_en": "mantua",
"url": "ita"
},
]
}
From the JSON response get the milano city's code & value_it
Ex: code=15146 & value_it=milano
Step2:
Construct the URL using retrieved values.
Ex: http://www.meteo.it/meteo/value_it-code
http://www.meteo.it/meteo/milano-15146
Example for Comacchio city:
Request URL : http://www.meteo.it/autosuggest/c.json
JSON Response:
{
"code": "38006",
"value": "Comacchio (FE)",
"value_it": "comacchio",
"value_en": "comacchio",
"url": "ita"
}
Construct URL using JSON values:
http://www.meteo.it/meteo/comacchio-38006

String city = "Bari";
String url = "https://www.ilmeteo.it/meteo/cerca?citta="+ city;
Document doc = Jsoup.connect(url).get();
List<Element> rows = doc.select("table[class=datatable] > tbody > tr[id*='']");
for (Element row : rows) {
System.out.println(row.text());
}
Sample Output:
13 pioggia e schiarite 25.3° NW 35 / 36 forte51% 0.1 mm modeste0% 3070m1008mb 27°>10km buona 51 7.9
14 pioggia e schiarite 25.5° NW 34 / 35 forte50% 0.1 mm modeste0% 3050m1008mb 27°>10km buona 50 7.6
15 poco nuvoloso 25.5° NW 32 / 33 forte50% - assenti -0% 3070m1008mb 27°>10km buona 50 6.5
16 poco nuvoloso 25.4° NW 31 / 32 forte49% - assenti -0% 3100m1008mb 27°>10km buona 49 5.1
17 sereno 25° NW 30 / 31 moderato50% - assenti -0% 3130m1008mb 26°>10km buona 50 3.3
18 sereno 24.5° NNW 28 / 29 moderato52% - assenti -0% 3180m1008mb 25°>10km buona 52 1.6
19 sereno 23.7° NW 26 / 27 moderato54% - assenti -0% 3220m1009mb 24°>10km buona 54 0.4
20 sereno 22.6° NW 23 / 26 moderato57% - assenti -0% 3190m1009mb 23°>10km buona 57 0
21 sereno 21° NW 21 / 24 moderato68% - assenti -0% 3160m1009mb 21°>10km buona 68 0
22 poco nuvoloso 20° NW 19 / 24 moderato76% - assenti -0% 3130m1009mb 20°>10km buona 76 0
23 nubi sparse 19.4° WNW 18 / 24 moderato80% - assenti -0% 3130m1009mb 20°>10km buona 80 0
24 nubi sparse 19.2° WNW 18 / 23 moderato81% - assenti -0% 3130m1009mb 20°>10km buona 81 0
01 poco nuvoloso 19° WNW 18 / 22 moderato82% - assenti -0% 3140m1009mb 20°>10km buona 82 0
02 poco nuvoloso 18.6° WNW 17 / 21 moderato84% - assenti -0% 3160m1009mb 19°>10km buona 84 0

Related

Parse html tag from json response in kotlin

Hey I am working in json response with html tag in kotlin. I don't understand how to parse html tag in response. I tried to work with retrofit and moshi. My response will look like this
Response
{
"items": [
{
"id": "1",
"date": "2021-11-01",
"title": "<html><head><style>body {
color: black;
}
</style></head><body><h1 id="sample-markdown">Sample Markdown</h1>
<p>This is some basic, sample markdown.</p>
<h2 id="second-heading">Second Heading</h2>
<ul>
<li>Unordered lists, and:<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
</li>
<li>More</li>
</ul>
<blockquote>
<p>Blockquote</p>
</blockquote>
<p>And <strong>bold</strong>, <em>italics</em>, and even <em>italics and later <strong>bold</strong></em>. Even <del>strikethrough</del>. A link to somewhere.</p>
<p>And code highlighting:</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> foo = <span class="hljs-string">'bar'</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">baz</span><span class="hljs-params">(s)</span> </span>{
<span class="hljs-keyword">return</span> foo + <span class="hljs-string">':'</span> + s;
}
</code></pre>
<p>Or inline code like <code>var foo = 'bar';</code>.</p>
<p>Or an image of bears</p>
<p><img src="http://placebear.com/200/200" alt="bears"></p>
<p>The end ...</p>
</body></html>"
}
]
}
I want to show title in textview.
Is it possible in android we can do?
You can render those html codes into a text view, try to use this library to do that.
https://github.com/SysdataSpA/SDHtmlTextView
Edit:
You can also use WebView to render html.

jsoup basic scraping technique

I have checked through all forum but i dont understand where i am wrong. basically i try to scrape the word "Sun, 04 Feb 2018" out. anyhelp will be appreciated on what concept i got wrong in this case but i keep getting no null return.
aspx. code
<div class="divLatestDraws slider" data-min-width="282">
<div class="slide-wrapper four-d">
<ul class="slide-container ulDraws" style="width: 1808px; margin-left: 0px;">
<li style="width: 301.33px;"><div class="tables-wrap">
<table class="table table-striped orange-header">
<thead>
<tr>
<th class="drawDate">Sun, 04 Feb 2018</th>
my jsoup code
// Connect to the web site
Document document = Jsoup.connect(url).get();
// Using Elements to get the Meta data
Elements xxx = document.select("div[class=divLatestDraws slider]");
Elements zzz = xxx.select("th[class=drawDate]");
desc=zzz.body().text();
} catch (IOException e) {
e.printStackTrace();
}
return null;

How to get webcam video file url from Weather Underground response in Android using JSOUP?

I would like to be able to play the webcam video response in a video view but the json response from Weather Underground for the "camurl" is like so:
http://www.wunderground.com/webcams/cadot1/902/show.html
The url for the video I need to play is embedded in the html code with a url like so:
https://www.wunderground.com/webcams/cadot1/1216/video.html?month=11&year=2016&filename=current.mp4
Is there a way to get that url from the json response "camurl"? I've heard of this term "html scraping", is that possible to get the embedded video url from the json response html page?
This is what the full json response looks like for the webcam:
{
"handle": "mahouser",
"camid": "mahouserCAM1",
"camindex": "1",
"assoc_station_id": "KCACAMAR18",
"link": "http://",
"linktext": "Michael Houser",
"cameratype": "Foscam FI9900P",
"organization": "",
"neighborhood": "Camarillo Hills",
"zip": "93010-12",
"city": "CAMARILLO",
"state": "CA",
"country": "US",
"tzname": "America/Los_Angeles",
"lat": "34.24947357",
"lon": "-119.03993988",
"updated": "2016-11-10 20:57:24",
"updated_epoch": "",
"downloaded": "2016-11-08 20:38:48",
"isrecent": "1",
"CURRENTIMAGEURL": "http://icons.wunderground.com/webcamramdisk/m/a/mahouser/1/current.jpg?t=1478812080",
"WIDGETCURRENTIMAGEURL": "http://icons.wunderground.com/webcamramdisk/m/a/mahouser/1/widget.jpg?t=1478812080",
"CAMURL": "http://www.wunderground.com/webcams/mahouser/1/show.html"
}
I've looked at jsoup and read the documentation but can't figure out how to get the needed url. Here is how the url looks in the html:
<td class="day">
<div class="row">
<div class="small-2 medium-5 columns">
<a href="/history/airport/KAJO/2016/11/15/DailyHistory.html" class="day-num">
15
</a>
</div>
<div class="small-10 medium-7 columns">
<img src="//icons.wxug.com/i/c/v4/clear.svg" alt="Clear" class="right" />
</div>
</div>
<div class="calThumb">
<a href="http://icons.wunderground.com/webcamramdisk/c/a/cadot1/902/current.jpg?1479239986" rel="lightbox[webcam]" title="">
<img src="http://icons.wunderground.com/webcamramdisk/c/a/cadot1/902/current-thumb.jpg?1479239986" width="100" height="75" alt="" title="Click to view the time-lapse video for this day." />
</a>
</div>
<p>View Video</p>
</td>
How can I get that "current.mp4" url from within the html code?
There are a lot of possible ways, but here is a simple solution:
Retrieve the html code with jsoup:
Document doc = Jsoup.connect("http://www.wunderground.com/webcams/cadot1/902/show.html").get();
Then, retrieve all elements with the class videoText:
Elements elements = doc.getElementsByClass("videoText");
This will give you a list of entries. Now simply select the one that ends with current.mp4.
To retrieve the current.mp4 URL:
for (Element link : elements) {
String linkHref = link.attr("href");
// linkHref contains something like video.html?month=11&year=2016&filename=current.mp4
// TODO check if linkHref ends with current.mp4
}

How Get The text on a separate line with JSOUP in Android?

A part of my HTML in my site (http://example.com) is:
//if my HTML code is:
<div class="text-co">
<div class="title">
00
11
22
</div>
</div>
<div class="text-co">
<div class="title">
33
44
55
</div>
</div>
and my android code is:
String url = "http://example.com";
ProgressDialog mProgressDialog;
#Override
protected Void doInBackground(Void... params) {
try {
Document document = Jsoup.connect(url).get();
Elements description = document.select("div[class=title] a");
desc = description.text();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
and I want to show '00' in first line and '11' in 2th line and so on.
For example: 00 11...
try:
Elements description = document.select("div[class=title]");
Elements aTags = description.select("a");
for(Element tag : aTags) {
String value = tag.text();
}
It's work with u?

Html parsing using Jsoup selector combinations in Android

I want to parse <dt>Seeders:</dt> & <dt>Leechers:</dt> from a html using Jsoup.
See the full code below.
<div id="details">
<dl class="col1">
<dt>Type:</dt>
<dd>Audio > Music</dd>
<dt>Files:</dt>
<dd><a href="/torrent/8682317/" title="Files" onclick="
if (filelist < 1) {
new Ajax.Updater('filelistContainer', '/ajax_details_filelist.php', {method: 'get', parameters: 'id=8682317'});
filelist=1;
}; toggleFilelist(); return false;">28</a></dd>
<dt>Size:</dt>
<dd>222.65 MiB (233468815 Bytes)</dd>
<br />
<dt>Tag(s):</dt>
<dd>markus schulz dakota things trance armada 2011 inspiron </dd>
<br />
<dt>Uploaded:</dt>
<dd>2013-07-13 15:30:25 GMT</dd>
<dt>By:</dt>
<dd>
-inspiron- <img src="/static/img/vip.gif" alt="VIP" title="VIP" style="width:11px;" border='0' /></dd>
<br />
<dt>Seeders:</dt>
<dd>16</dd>
<dt>Leechers:</dt>
<dd>1</dd>
<dt>Comments</dt>
<dd><span id="NumComments">0</span>
</dd>
<br />
<dt>Info Hash:</dt><dd> </dd>
01DD6B7325C3DB5F0DF5BBE510FD3FD9738D1C88 </dl>
<div class="torpicture">
<img src="//image.bayimg.com/345b5b11734bb9973863359cc52929f3ddc45205.jpg" title="picture" alt="picture" />
</div>
<dl class="col2">
</dl>
<div id="CommentDiv" style="display:none;">
<form method="post" id="commentsform" name="commentsform" onsubmit="new Ajax.Updater('NumComments', '/ajax_post_comment.php', {evalScripts:true, asynchronous:true, parameters:Form.serialize(this)}); return false;" action="/ajax_post_comment.php">
<p class="info">
<textarea name="add_comment" id="add_comment" rows="8" cols="50"></textarea><br/>
<input type="hidden" name="id" value="8682317"/>
<input type="submit" value="Submit" /><input type="button" value="Hide" onclick="document.getElementById('CommentDiv').style.display = 'none'" />
</p>
</form>
</div>
<br/>
<br/>
<div id="social">
</div>
<iframe src="http://cdn1.adexprt.com/dl/dl.php?b=bar&r=75&n=Markus_Schulz_-_Global_DJ_Broadcast_%282013-07-11%29_%28Inspiron%29&m=magnet%3A%3Fxt%3Durn%3Abtih%3A01dd6b7325c3db5f0df5bbe510fd3fd9738d1c88%26dn%3DMarkus%2BSchulz%2B-%2BGlobal%2BDJ%2BBroadcast%2B%25282013-07-11%2529%2B%2528Inspiron%2529%26tr%3Dudp%253A%252F%252Ftracker.openbittorrent.com%253A80%26tr%3Dudp%253A%252F%252Ftracker.publicbt.com%253A80%26tr%3Dudp%253A%252F%252Ftracker.istole.it%253A6969%26tr%3Dudp%253A%252F%252Ftracker.ccc.de%253A80%26tr%3Dudp%253A%252F%252Fopen.demonii.com%253A1337" width="622" height="51" frameborder="0" scrolling="no"></iframe>
<br /><br /> <div class="download">
<a style='background-image: url("/static/img/icons/icon-magnet.gif");' href="magnet:?xt=urn:btih:01dd6b7325c3db5f0df5bbe510fd3fd9738d1c88&dn=Markus+Schulz+-+Global+DJ+Broadcast+%282013-07-11%29+%28Inspiron%29&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.istole.it%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337" title="Get this torrent"> Get this torrent</a>
<a style='background-image: url("/static/img/icon-https.gif");' href="http://adexprt.me/get/Markus_Schulz_-_Global_DJ_Broadcast_%282013-07-11%29_%28Inspiron%29?tag=bal" title="Anonymous Download"> Anonymous Download</a>
</div>
<div>(Problems with magnets links are fixed by upgrading your torrent client!)</div>
<div class="nfo">
<pre>=======================================================
Site: http://www.inspirontrance.com/
=======================================================
=======================================================
F B Page: Inspiron Trance
=======================================================
=======================================================
TWITTER : inspiron22
=======================================================
Markus Schulz
01. Mobil - One Morning (Aleksey Sladkov Remix)
02. Store N Forward - Nuts
03. Alter Future vs. Holbrook & SkyKeeper - Megapolis
04. Danilo Ercole - Cruzer
05. Aaron Camz - Emission
06. Markus Schulz Featuring Sarah Howells - Tempted
07. M.I.K.E. Presents Caromax - Inner Thoughts
08. Ruffault - Progressive Dream
09. Styller - What We Left Behind
10. Meridian - Exit
11. Lange - A Different Shade of Crazy
12. Tucandeo Featuring Natalie Gioia - Disappear (Xtigma Remix)
13. Sebastian Weikum - Sky is the Limit
14. Markus Schulz - Don't Leave Until the Sunrise
Guy J
01. Roger Martinez & Secret Cinema - Menthol Raga (Guy J Remix)
02. Ambassador - The Fade (Guy J Remix)
03. Guy J - Seven
04. Echomen – Perpetual (Guy J Remix)
Back with Markus Schulz
15. Mauro Picotto & Riccardo Ferri - New Time, New Place (New World Punx Remix)
16. Grube & Hovsepian - Trickster
17. Nifra - Waves
18. Markus Schulz featuring Dauby - Perfect (Digital X Remix) [Global Selection]
19. Basil O'Glue - Gilgamesh
20. Skytech - The Other Side
21. ID
Enjoy
(Inspiron) </pre>
</div>
I've used this code which parses the whole details instead of parsing the 'seeders' & 'leechers'
try {
document = Jsoup.connect(BLOG_URL).get();
title = document.title();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// selector query
Elements nodeBlogStats = document.select("div#details");
// check results
if (nodeBlogStats.size() > 0) {
// get value
result = nodeBlogStats.get(0).text();
}
According to http://jsoup.org/apidocs/org/jsoup/select/Selector.html, you are looking for
E ~ F an F element preceded by sibling E
and
:contains(text) elements that contains the specified text.
I would try
Element seeders = document.select("dt:contains(Seeders) ~ dd").get(0);
Element leechers = document.select("dt:contains(Leechers) ~ dd").get(0);

Categories

Resources