HTML Parsing using Jsoup.Jar
-
08-10-2019 - |
Question
Document doc = Jsoup.connect("http://reviews.opentable.com/0938/9/reviews.htm").get();
Element part = doc.body();
Elements parts = part.getElementsByTag("span");
String attValue;
String html;
for(Element ent : parts)
{
if(ent.hasAttr("class"))
{
attValue = ent.attr("class");
if(attValue=="BVRRReviewText description")
{
System.out.println("\n");
html=ent.text();
System.out.println(html);
}
}
}
Am using Jsoup.jar for the above program.
I am accessing the webpage and my aim is to the print the text that is found within the tag <span class="BVRRReviewText description">text</span>
.
But nothing is getting printed as output. There is no contents added to the String html
in the program. But attValue
is getting all the attribute values of the span tag.
Where must I have went wrong? Please advise.
Solution
if(attValue=="BVRRReviewText description")
should be
if(attValue.equals("..."))
surely?
This is Java, not Javascript.
OTHER TIPS
Change
attValue=="BVRRReviewText description"
for
attValue.matches("...")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow