简体   繁体   English

如何使用Jsoup和/或正则表达式Java从URL删除html标签

[英]how to remove html tags from URL using Jsoup and/or regular expressions Java

I need to remove HTML tags from a url using Jsoup and/or Regular Expressions in Java. 我需要使用Java中的Jsoup和/或正则表达式从网址中删除HTML标签。 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; 到目前为止,我已经尝试了一些方法,使用javax.swing.text.html.HTMLEditorKit甚至Jsoup,但是我不能交换导入的java.io.FileReader。 to import java.io.InputStreamReader; 导入java.io.InputStreamReader; import java.net.URL; 导入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));
      }


}

Using Jsoup 使用Jsoup

public static String htmlremoved(String html) {
    return Jsoup.parse(html).text();
}

Using Regex String nohtml = YourUrlString.toString().replaceAll("\\\\<.*?>",""); 使用正则表达式String nohtml = YourUrlString.toString().replaceAll("\\\\<.*?>","");

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(); 添加Jsoup.parse(String).text(); to the String line = br.readLine() s print command like this System.out.println( Jsoup.parse(line).text()); String line = br.readLine()的打印命令,例如System.out.println( Jsoup.parse(line).text()); and this code takes out the HTML tags. 然后这段代码将HTML标记删除。 Of course first you have to declare the method public static String htmlremoved(String html) { return Jsoup.parse(html).text(); } 当然,首先您必须声明方法public static String htmlremoved(String html) { return Jsoup.parse(html).text(); } public static String htmlremoved(String html) { return Jsoup.parse(html).text(); } . public static String htmlremoved(String html) { return Jsoup.parse(html).text(); } You can also add the Jsoup.parse(String).text(); 您还可以添加Jsoup.parse(String).text(); code to a bw.write . 代码到bw.write

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM