简体   繁体   中英

jsoup retrieving text from a div with only a class

I'm trying to retrieve the text "Casual Dining" from the webpage

https://www.opentable.com//r/romanos-macaroni-grill-fort-collins?avt=eyJ2IjoxLCJtIjoxLCJwIjowLCJjIjowfQ&corrId=2b2727e6-ad4c-4958-90a6-b9f55623ab1c

I've been using:

Document mainPage = Jsoup.connect(addTo.getUrl()).userAgent("Mozilla").get();
    Elements content = mainPage.select("div._16c8fd5e._1f1541e1");  
                Element link = content.first();
                String out = link.text();

While that doesn't give me an error, it also doesn't give me the text I want.

I'm doing this for multiple similar webpages automatically, so as long as the class is the same I can get the type of restaurant text for each webpage.

How exactly do I narrow the focus of what I'm scraping down to just that text when I only have a class to go on?

there are multiple divs that have ._16c8fd5e ._1f1541e1 classes in that web, then you select all those divs with your select, not only one. Instead you can search by text content like this

String text = mainPage.select("span:contains(Dining Style)")
                   .parents().get(0).siblingElements().get(0)
                   .selectFirst("div").text();
System.out.println(text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM