Java Reference
In-Depth Information
To see how this works, the next exercise accesses Twitter again, but this time using screen scraping
techniques only. Create a class named TwitterScraper with the following source code:
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class TwitterScraper {
private static final Map<String, String> COOKIES =
new HashMap<String, String>();
private static final String USERNAME = "FILL IN YOUR USERNAME";
private static final String PASSWORD = "FILL IN YOUR PASSWORD";
public static void main(String[] args) throws IOException {
System.out.println("Trying to log into Twitter...");
boolean result = login(USERNAME, PASSWORD);
System.out.println("Cookies are now:");
System.out.println(COOKIES);
if (!result) {
System.out.println("Login failed! Try the following:");
System.out.println("- Has the Twitter website changed?");
System.out.println("- Are your username and password correct?");
System.exit(0);
}
String username = getUsername();
System.out.println("Your username is: "+username);
String[] myStats = getStats(username);
System.out.println("Your stats: ");
System.out.println("- Tweets: "+myStats[0]);
System.out.println("- Following: "+myStats[1]);
System.out.println("- Followers: "+myStats[2]);
String[] wileyStats = getStats("WileyTech");
System.out.println("@WileyTech stats: ");
System.out.println("- Tweets: "+wileyStats[0]);
System.out.println("- Following: "+wileyStats[1]);
System.out.println("- Followers: "+wileyStats[2]);
}
public static String[] getStats(String username) throws IOException {
Connection connection = Jsoup.connect(" https://twitter.com/"+username);
// We do not need to be logged in to get stats
Document result = connection.get();
String[] stats = new String[3]; // tweets, following, followers
try {
Element statsTable = result.select("table.js-mini-profile-stats").get(0);
Search WWH ::




Custom Search