I am trying to develop an instagram scraper; this is my code:
try {
System.out.println("search in https://instagram.com/" + txtUsername.getText() + "?__a=1");
URLConnection connection = new URL("https://instagram.com/" + txtUsername.getText() + "?__a=1").openConnection();
/*connection
.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");*/
connection
.setRequestProperty("Cookie",
"sessionid=XXXXXXXXXXXXXXXXXXXXX"); //setting cookie
connection.connect();
BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream(),
Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
String line;
while (r.readLine() != null) {
sb.append(r.readLine());
}
System.out.println(sb.toString());
} catch (MalformedURLException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
I am therefore trying to set a session cookie to simulate a login and be able to view a user's page in order to get the data (followers, following etc. from this link https://www.instagram.com/username/?__a=1 ). The problem is that the cookie is not set and in fact what I receive in output on the console is the source code of the instagram login page, this means that the cookie did not exist (or that the session is wrong but I'm sure it's right ). How can I solve this problem and then set the cookie?