2
I would like to get the names of the companies that appear in a search like "Farmacias em Santo Andre" on Google Maps.
Erro: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:458)
at crawlergooglemap.CrawlerGoogleMap.main(CrawlerGoogleMap.java:48)
C:\Users\Eugene\AppData\Local\NetBeans\Cache\11.1\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\Eugene\AppData\Local\NetBeans\Cache\11.1\executor-snippets\run.xml:68: Java returned: 1 BUILD FAILED (total time: 7 seconds)
public static void main(String[] args) throws IOException, XmlPullParserException {
String url;
Scanner s = new Scanner(System.in);
System.out.println("Cole a URL do maps:");
url = s.next();
Document page = Jsoup.connect(url).get();
for(int i = 0; i < 20; i++){
String empresa = page.getElementsByAttribute("section-result-title").get(0).selectFirst("h3").text();
if(empresa.length() != 0){
System.out.println(empresa);
}
else{
System.out.println("0");
}
}
}
Part of the website’s HTML:
<div class="section-result-title-container">
<h3 class="section-result-title">
<span jstcache="134">Farmácia Nazaré</span>
<button jstcache="135" style="display:none"></button>
</h3>
<span jstcache="136" class="section-ads-placecard" style="display:none">Anúncio</span>
<span jstcache="137" class="section-hotel-ads-url" style="display:none">Anúncio</span>
If you made that mistake, it’s because
getElementsByAttribute
did not find anything and returned an empty list. In this case, simply check before if the list is empty (usingisEmpty()
, for example)– hkotsubo
Blz, this "solved", now I realized that the program does not take any information, the isEmpty() always of the 1
– Eugenio Maria
If it is empty, then there is no element with the attribute
section-result-title
. But if you want to get the results, maybe you should use the Google Maps API instead of accessing HTML and trying to extract data from it. Note: I did not test this link, it was one that appeared in the search results, but you can look for others, if you want. Accessing the API directly seems simpler than unlocking the HTML (even if the API has access limits and from a certain amount it charges)– hkotsubo
In this case I was taking a class with this name and using get Attribute, but I already changed to get class and it won’t go tb, and the class ta la yes, look here a part of the site, (and thanks for the API): <div class="Section-result-title-container"> <H3 class="Section-result-title"> <span jstcache="134">Nazaré Pharmacy</span> <button jstcache="135" style="display:None"></button> </H3> <span jstcache="136" class="Section-ads-placecard" style="display:None">Ad</span> <span jstcache="137" class="Section-hotel-ads-url" style="display:None">Ad</span>
– Eugenio Maria