Reputation: 317
I've got a bit of an issue, which I've spent quite some time now trying to fix and search solutions for but to no avail. So I hope you guys can perhaps understand what I'm trying to achieve and have some suggestions?
Basically, I'm trying to write a program that will allow the user to type in a word, the program will then go to dictionary.reference.com, search the word, then retrieve the .mp3 hyperlink (or open it) and allow the user to listen to how the word is pronounced. So basically it is a word pronunciation program.
My code is still in very early stages, but my sketchy idea is that I'll have a GUI for them to enter the word, so don't mind the debugging fixed variables (such as searchWord).
Code below:
import java.net.*;
import java.io.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class Pronun {
String searchWord = "development"; // Fixed variable for debugging
Element audio;
public Pronun() throws Exception {
// As program is ran, begin searching the word in the dictionary.
URL makeSearch = new URL("http://dictionary.reference.com/browse/"+searchWord+"?s=t");
URLConnection connectTo = makeSearch.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connectTo.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
File input = new File("output.txt");
Document doc = Jsoup.parse(input, "UTF-8");
Element audio = doc.select("audspk.classname").first();
// Write the hyperlink to the file "output.txt".
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt", true)));
out.println(audio);
out.close();
in.close();
}
}
The hyperlink I'm trying to retrieve from the page, is:
<a class="audspk" href="http://static.sfdict.com/dictstatic/dictionary/audio/luna/D02/D0238600.mp3">
As you can see, I'm trying to get it through Jsoup, using it's classname - "audspk".
But basically, when I'm running the code this is the error I'm receiving:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from Elements to String
at URLConnectionReader.main(URLConnectionReader.java:25)
I've attempted to convert it via a toString, but this also doesn't seem to work, but I must be doing something wrong there, so any help on this would be appreciated. I hope you can understand what I'm trying to achieve here.
Upvotes: 2
Views: 822
Reputation: 2538
I have a few comments:
output.txt
seems to be empty because you didn't write anything to it.If you want to print the link then use method attr
of Element
:
String link = doc.select(".audspk").first().attr("href");
jsoup
can download and parse URL
by himself:
URL makeSearch = new URL("http://dictionary.reference.com/browse/" + searchWord + "?s=t");
Document doc = Jsoup.parse(makeSearch, 1000);
String link = doc.select(".audspk").first().attr("href");
System.out.println(link);
Upvotes: 1