How to extract text from inter DIV with Jsoup? - android

I am trying to extract a text from a div class inside of a main div class. One example of a code snippet i am trying to use is:
Eg.
<div class="main">
<div class="01"></div>
<div class="02"></div>
<div class="03">
<span class=""></span>
<div class="content"> **TEXT TO EXTRACT** </div>
</div>
</div>
My code:
Document doc = Jsoup.connet(url).get();
Elements title01 = doc.select("div.main > div > div > div");
txt01 = title01.text();
This code don't work. What is wrong?

The solution is very simple:
Elements elements = doc.select("div.main div.content");
If the content div doesn't have to be within a main div you can do this instead:
Elements elements = doc.select("div.content");
Why your code doesn't work
div.main get every div with class main
CurrentCollection = {main}
<div class="main">
......
</div>
> div searches div.main for div children
CurrentCollection = {01, 02, 03}
<div class="01"></div>
<div class="02"></div>
<div class="03">.....</div>
> div seaches every element in CurrentCollection for div children
CurrentCollection = {content}
<div class="content"> **TEXT TO EXTRACT** </div>
> div searches every element in CurrentCollection for div children
CurrentCollection = {}

Related

how to get image from the following scipt through jsoup

I try this link
jSoup to check if a span class exists
but i want to get image url from the following script through jsoup.
<ul class="a-unordered-list a-nostyle a-button-list a-vertical a-spacing-top-micro">
<li class="a-spacing-small item"><span class="a-list-item">
<span class="a-declarative" data-action="thumb-action" data-thumb-action="{"variant":"MAIN","index":"0"}">
<span class="a-button a-button-selected a-button-thumbnail a-button-toggle a-button-focus"><span class="a-button-inner"><input class="a-button-input" type="submit"><span class="a-button-text" aria-hidden="true">
<img alt="" src="https://images-na.ssl-images-amazon.com/images/I/31XcCgGBePL._SS40_.jpg">
</span></span></span>
</span>
</span></li>
Try This :
Elements anchors = doc.select("li span img");

Foundation 6 Mobile Search Bar Sticking to Bottom on Android

When navigating the site http://emich.edu on Android devices (with any browser), the sticky mobile navigation menu sticks to the bottom of the page if one clicks through to another page, scrolls to the bottom of that page, and clicks the back button. A picture of the issue is attached.
This should not happen; the nav should always be under the site header. We are using Foundation 6.3 and have tried several other versions. We have even tried Foundation outside of this site with no extra markup, and the same result is produced. I can't seem to find any other threads that describe a similar problem. Could it be a problem with Android?
The phones we have tested use Android v. 7.0 and 6.0.1.
To be clear, this problem does not happen on Apple devices nor PCs.
<header class="wide-image">
<div class="brand front-top">
<div class="row top-row" data-equalizer="branding">
<div class="large-4 medium-12 small-12 columns logo-combine" data-equalizer-watch="branding">
<div class="front-title">
<img src="{{f:57957}}" alt="Eastern Michigan University logo" />
</div>
</div>
<div class="large-8 medium-12 show-for-large columns" data-equalizer-watch="branding">
<div class="row idrow front">
<div class="small-12 columns secondary-nav">
<xsl:copy-of select="ou:includeFile('/_resources/inc/site-search.inc')" />
<ul id="quick-links-top">
<xsl:copy-of select="ou:includeFile('/_resources/inc/quick-links.inc')" />
<li id="search-glass"><span class="visuallyhidden">Search</span><i class="fa fa-search" aria-hidden="true"></i></li>
</ul>
</div>
</div>
</div>
</div>
<div id="site-title" class="row">
<div class="small-12 columns">
<ouc:div label="site-name" button-text="Site Name" group="_Admin" path="{$ou:siteroot}/_site/inc/name.pcf">
<xsl:if test="$ou:action = 'pub' and $ou:siteroot != '/'"><xsl:copy-of select="ou:includeFile(concat($ou:siteroot,'/_site/inc/name.inc'))" /></xsl:if>
</ouc:div>
</div>
<xsl:if test="($ou:topnav!='off')">
<div class="small-12 columns skip-main-nav">
<a class="show-on-focus show-for-sr" href="#maincontent">Skip Global Navigation</a>
</div>
</xsl:if>
</div>
<div data-sticky-container="">
<div data-sticky="" data-top-anchor="site-title:bottom" data-options="marginTop:0;" style="width:100%">
<div class="title-bar" data-responsive-toggle="example-animated-menu" data-hide-for="large">
<button class="menu-icon" type="button" data-toggle="offCanvasLeft" aria-expanded="false" aria-controls="offCanvasLeft">
<div class="visuallyhidden">Open/Close Menu</div>
</button>
<div class="title-bar-title" data-toggle="offCanvasLeft">Search & Navigation</div>
</div>
<!-- If top nav, add bar -->
<xsl:if test="$ou:topnav='on' or $ou:topnav='true' or $ou:topnav='1' or $ou:topnav='yes'"><!-- Top nav is truthy? -->
<div id="main-nav">
<div class="row show-for-large collapse">
<div class="small-12 columns">
<nav class="menu-box {if ($ou:action != 'pub') then 'omni-primary-nav' else ''}"><!-- 'omni-primary-nav' class used to style nav list within OUC preview -->
<ouc:div label="top-nav" button-text="Navigation" group="_Admin" path="{$ou:siteroot}/_site/inc/navigation.pcf">
<xsl:if test="$ou:action = 'pub'">
<xsl:text disable-output-escaping="yes"><? echo $navigation->primaryMenu; ?></xsl:text>
</xsl:if>
</ouc:div>
</nav>
</div>
</div>
</div>
</xsl:if>
</div>
</div>
</div>
...a bunch of XSL
</header>
It seems by default, the elements aren't sticky for small screens.
Sticky for small screens can be turned on by adding
data-options="stickyOn:small;"
or
data-sticky-on="small"
To the element data-sticky attribute.
Seems the reason it isn't sticky by default on "small" is to "save real estate".
Methinks it is this bug. See the workarounds mentioned there. Moving discussion there...

JSoup not properly extracting elements by class

I have the following element in a webpage:
<div id="pnNij" class="post" data-tag1="" data-tag2="">
<a class="image-list-link" href="http://imgur.com/gallery/pnNij" data-page="0">
<img alt="" src="./Imgur_ The most awesome images on the Internet_files/H7fZCNgb.jpg">
<div class="point-info gradient-transparent-black transition">
<div class="relative">
<div class="pa-bottom">
<div class="arrows">
<div title="like" class="pointer arrow-up icon-upvote-outline" data="pnNij" type="image" data-up="4212"></div>
<div title="dislike" class="pointer arrow-down icon-downvote-outline" data="pnNij" type="image" data-downs="502"></div>
<div class="clear"></div>
</div>
<div class="point-info-points" title="points">
<span class="points-pnNij">3,710</span>
<span class="points-text-pnNij">points</span>
</div>
</div>
</div>
</div>
</a>
<div class="hover">
<p>Seems like 2017 has it all...</p>
<div class="post-info">
album · 69,542 views
</div>
</div>
</div>
notice how the href is equal to http://imgur.com/gallery/pnNij.
However, when I use JSoup to extract elements from the page like this:
docImgur = Jsoup.connect("http://imgur.com/").get();
Elements links = docImgur.getElementsByClass("post");
The element is almost extracted properly, except the href attribute is equal to /gallery/pnNij/
Why does the href attribute not contain the full URL?
When you check the page source, you'll find
<a class="image-list-link" href="/gallery/WRzti" data-page="0">
...
</a>
So the href attribute is not absolute, which results in your expected results: /gallery/WRzti
Solution
Use the abs: attribute prefix.
Example
Document docImgur = Jsoup.connect("http://imgur.com/").get();
Elements links = docImgur.select("a[href].image-list-link");
for (Element element : links) {
System.out.println(element.attr("abs:href"));
}
Output
http://imgur.com/gallery/WRzti
http://imgur.com/gallery/tCnDJ
http://imgur.com/gallery/JIHYh
...

Parse only content from bold tag Jsoup

<div class="item all clearfix">
<div class="pic all">
<a href="/terrace.jpg"/></a>
</div>
<div class="text all">
<a href="/link/test" class="title">
Some random title
</a>
<strong>STRONG TEXT</strong>
Some random subtitle…
</div>
</div>
I'm trying to get the text from the bold tag only, but whenever I parse it, it shows the complete. I also tried to remove the a class, but there was text left out below - "Some random subtitle".
I tried by:
Element strong_title = el.select("strong").first();
And then by retrieving strong_title.text().
Nevermind, it was a project-related code mistake.
The following code:
document.select("strong"); works just fine.

How to show data in a collapsible list dynamically in phonegap?

I'm developing my first phonegap application. I am trying to get some datas from a web server and then show them in the collapsible list. However, I couldn't add datas to list dynamically. Additionally, the list structure is shown in web page but i couldn't see anything on the android emulator. I just see black page on it. How can solve these problems? Can anyone help about that? Thank you in advance.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.
mobile-1.3.2.min.css">
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"
></script>
</head>
<body>
jQuery Mobile Collapsibles
<div id = "outer" data-role="content">
<div id="list" data-role="collapsible-set">
<div id="first" data-role="collapsible">
<h3>Güvenlik</h3>
<p id="guvenlik">I'm the expanded content.</p>
</div>
<div id="second" data-role="collapsible">
<h3>Sağlık</h3>
<p id="saglik">I'm the expanded content.</p>
</div>
<div id="third" data-role="collapsible">
<h3>Diğer Servisler</h3>
<p id="diger">I'm the expanded content.</p>
</div>
</div>
<button id="Sorgula">Sorgula</button>
</div>
<script>
$(document).ready(function()
{
...
});
</script>
</body>
</html>
You can insert items from list like I did, or you can just write it as inside the each function.
The find call at the end is just to close them when it ends.
$.each(list, function(index, value) {
$("#container").append('<div data-role="collapsible" data-iconpos="right" class="ui-collapsible ui-collapsible-inset ui-corner-all ui-collapsible-themed-content ui-collapsible-collapsed ui-first-child ui-last-child">'+
'<h3 class="ui-collapsible-heading ui-collapsible-heading-collapsed">'+value.name+
'<ul data-role="listview">'+some_li_list+'</ul>'+
'</div>');
});
$('#container').find('div[data-role=collapsible]').collapsible();

Categories

Resources