-2

Unable to fetch the text from the div... Using below code, Not sure whats the issue

try {
    connection =  new URL("https://en-gb.facebook.com/Aeysunnna/videos/265359950651320/")
        .openConnection();
    Scanner scanner = new Scanner(connection.getInputStream(),"UTF-8");
    scanner.useDelimiter("\\A");
    content = scanner.next();
} catch (Exception ex) {
    ex.printStackTrace();
    System.out.println("error");
}

Document doc = Jsoup.parse(content);
Thread.sleep(10000);
Elements elements = doc.select("div._1t6k");
System.out.println(elements.text());
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
devloper
  • 47
  • 1
  • 8
  • It seems that you are using non-default APIs (JSoup). If so always state that in your question and use the appropriate tags. – Zabuzard Oct 30 '17 at 12:52
  • If an user answered your question please also accept his answer ([Accepting Answers: How does it work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)). If not then please specify what remains unanswered, this is a crucial part of StackOverflow, thank you very much. – Zabuzard Nov 14 '17 at 10:34

1 Answers1

0

It is probably because your Java browser is not logged in to Facebook. If you're not logged in you will see nothing except the login page. Thus you can not read the content of the div, you are currently at the starting page.

Try printing the whole content you're getting and check whether it is actually the starting page.


You find an in-depth explanation here: Finding a word in a web page using java

As shown there you will probably not find an easy fix for this issue.


Also note that scraping Facebook without using their own API violates their Terms of Service.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • I am able to see that div in the content after the HTML parse, But still not able to fetch the text of that div. – devloper Oct 30 '17 at 13:40
  • So the document `doc` contains the `div` with all its content when you print it? But `doc.select` doesn't select it? Please show the relevant content by **editing** your question. Your selector is probably incorrect or something like that. – Zabuzard Oct 30 '17 at 13:43
  • Yes doc contains that div but when i am using Elements elements = doc.select("div._1t6k"); its returning the elements size zero – devloper Oct 30 '17 at 13:49
  • I am getting this div aswell after HTML parse
    7 M Views
    – devloper Oct 30 '17 at 13:53