My objective is to access one homepage, parse it for a link, and then access that link to parse for a title. This is what I have so far:
try {
articleURL = LoadArticleURL(homeurl);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
articleTitle = LoadArticleTitle(articleURL);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and
//LOADS THE URL
private String LoadArticleURL(String homeURL) throws Exception {
try {
reader = new BufferedReader(new InputStreamReader(new URL(homeURL).openStream(), "UTF-8"));
for (String line; (line = reader.readLine()) != null;) {
builder.append(line.trim());
}
}
finally {
if(reader != null)
try {
reader.close();
}
catch (IOException logOrIgnore) {}
}
String start = "<span id=\"spot_hed\"><a href=\"";
String end = "\">";
String part = builder.substring(builder.indexOf(start) + start.length());
String returnURL = part.substring(0, part.indexOf(end));
return returnURL;
}
//LOADS THE TITLE
private String LoadArticleTitle (String articleURL) throws Exception {
try {
reader = new BufferedReader(new InputStreamReader(new URL(articleURL).openStream(), "UTF-8"));
for (String line; (line = reader.readLine()) != null;) {
builder.append(line.trim());
}
}
finally {
if(reader != null)
try {
reader.close();
}
catch (IOException logOrIgnore) {}
}
String start = "<title>";
String end = "</title>";
String part = builder.substring(builder.indexOf(start) + start.length());
String returnTitle = part.substring(0, part.indexOf(end));
return returnTitle;
}
Is there anything obvious that you guys can see that would divert the LoadArticleTitle() method to point to the wrong URL? Also if you know of a more efficient way of doing this whole project I'm all ears.