Reputation: 1
I need to remove HTML tags from a url using Jsoup and/or Regular Expressions in Java. so far I've tried a couple of stuff, using javax.swing.text.html.HTMLEditorKit and even Jsoup but I can't exchange the import java.io.FileReader; to import java.io.InputStreamReader; import java.net.URL; and get it to work successfully.
What else can I do?
Here is the code I have tried**
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.URL;
import java.io.IOException;
import java.io.FileReader;
import java.io.Reader;
import org.jsoup.Jsoup;
public class WebTest {
private WebTest() {}
public static String extractText(Reader reader) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(reader);
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
String textOnly = Jsoup.parse(sb.toString()).text();
return textOnly;
}
public static void main(String[] args) throws Exception {
String filename = "/Users//Desktop/file4.csv";
String urltodownload = "http://www.amazon.com";
URL url = new URL(urltodownload);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filename)));
String document = "";
while (br.ready()) {
String line = br.readLine();
document += line + "\n";
System.out.println( line );
} bw.close();
String file = "/Users/Desktop/file4.csv";
FileReader reader = new FileReader(file);
System.out.println(WebTest.extractText(reader));
}
}
Upvotes: 0
Views: 1175
Reputation: 1
Ok, so thanks to everyone who contributed. What I did that seems to have solved my problem is this. Adding the Jsoup.parse(String).text();
to the String line = br.readLine()
s print command like this System.out.println( Jsoup.parse(line).text());
and this code takes out the HTML tags. Of course first you have to declare the method public static String htmlremoved(String html) {
return Jsoup.parse(html).text();
}
. You can also add the Jsoup.parse(String).text();
code to a bw.write
.
Upvotes: 0
Reputation: 3904
Using Jsoup
public static String htmlremoved(String html) {
return Jsoup.parse(html).text();
}
Using Regex String nohtml = YourUrlString.toString().replaceAll("\\<.*?>","");
Upvotes: 1