首页 > 解决方案 > 减去部分文本

问题描述

我有这个代码

public void descargarURL() {
    try{
        URL url = new URL("https://www.amazon.es/MSI-Titan-GT73EVR-7RD-1027XES-Ordenador/dp/B078ZYX4R5/ref=sr_1_1?ie=UTF8&qid=1524239679&sr=8-1");
        BufferedReader lectura = new BufferedReader(new InputStreamReader(url.openStream()));
        File archivo = new File("descarga2.txt");
        BufferedWriter escritura = new BufferedWriter(new FileWriter(archivo));
        BufferedWriter ficheroNuevo = new BufferedWriter(new FileWriter("nuevoFichero.txt"));
        String texto;

        while ((texto = lectura.readLine()) != null) {
            escritura.write(texto);

            }
        lectura.close();
        escritura.close();
        ficheroNuevo.close();
        System.out.println("Archivo creado!");
        //}

    }
    catch(Exception ex) {
        ex.printStackTrace();
    }
}
public static void main(String[] args) throws FileNotFoundException, IOException {
    Paginaweb2 pg = new Paginaweb2();
    pg.descargarURL();
}

}

我想从 URL 中删除B078ZYX4R5的引用部分,以及这个实体 /

在保存在文本文件中的 html 之后,有一部分代码,*"<div id =" cerberus-data-metrics "style =" display: none; "data-asin =" B078ZYX4R5 "data-as-price = "1479.00" data-asin-shipping = "0" data-asin-currency-code = "EUR" data-substitute-count = "0" data-device-type = "WEB" data-display-code = "Asin is not eligible because it has a retail offer "> </ div>"*我只想从那里得到价格1479.00,它包含在标签“data-as-price =”中

我不想使用外部库,我知道可以通过拆分、索引和子字符串来完成

谢谢!!!!

标签: javasplitsubstringindexof

解决方案


您可以使用正则表达式来解决这两个任务。然而对于第二个任务(从 HTML 中提取价格),您可以使用JSOUP,它更适合从 HTML 中提取内容。

以下是一些基于正则表达式的可能解决方案,适用于您的任务:

1. 更改网址

private static String modifyUrl(String str) {
    return str.replaceFirst("/[^/]+(?=/ref)", "");
}

这只是使用正则表达式的替换,使用正向前瞻(?=/ref)(请参阅https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

提取价格

private static Optional<String> extractPrice(String html) {
    Pattern pat = Pattern.compile("data-as-price\\s*=\\s*[\"'](?<price>.+?)[\"']", Pattern.MULTILINE);
    Matcher m = pat.matcher(html);
    if(m.find()) {
        String price = m.group("price");
        return Optional.of(price);
    }
    return Optional.empty();
}

在这里,您还可以使用正则表达式 ( data-as-price\s*=\s*["'](?<price>.+?)["']) 来定位价格。(?<price>.+?)然后,您可以使用命名组 ( ) 提取价格。

我在Optional这里退回一个,以便您处理找不到价格的情况。

这是两种方法的简单测试用例:

public static void main(String[] args) throws IOException {
    String str = "https://www.amazon.es/MSI-Titan-GT73EVR-7RD-1027XES-Ordenador/dp/B078ZYX4R5/ref=sr_1_1?ie=UTF8&qid=1524239679&sr=8-1";
    System.out.println(modifyUrl(str));
    String html = "<div id =\" cerberus-data-metrics \"style =\" display: none; \"data-asin =\" B078ZYX4R5 \"data-as-price = \"1479.00\" data-asin-shipping = \"0\" data-asin-currency-code = \"EUR\" data-substitute-count = \"0\" data-device-type = \"WEB\" data-display-code = \"Asin is not eligible because it has a retail offer \"> </ div>";
    extractPrice(html).ifPresent(System.out::println);
}

如果您运行这个简单的测试用例,您将在控制台上看到以下输出:

https://www.amazon.es/MSI-Titan-GT73EVR-7RD-1027XES-Ordenador/dp/ref=sr_1_1?ie=UTF8&qid=1524239679&sr=8-1
1479.00

更新

如果您想从 URL 中提取参考,您可以使用与用于提取价格的代码类似的代码来完成。这是一种从模式中提取特定命名组的方法:

private static Optional<String> extractNamedGroup(String str, Pattern pat, String reference) {
    Matcher m = pat.matcher(str);
    if (m.find()) {
        return Optional.of(m.group(reference));
    }
    return Optional.empty();
}

然后您可以使用此方法提取参考和价格:

private static Optional<String> extractReference(String str) {
    Pattern pat = Pattern.compile("/(?<reference>[^/]+)(?=/ref)");
    return extractNamedGroup(str, pat, "reference");
}

private static Optional<String> extractPrice(String html) {
    Pattern pat = Pattern.compile("data-as-price\\s*=\\s*[\"'](?<price>.+?)[\"']", Pattern.MULTILINE);
    return extractNamedGroup(html, pat, "price");
}

您可以使用以下方法测试上述方法:

public static void main(String[] args) throws IOException {
    String str = "https://www.amazon.es/MSI-Titan-GT73EVR-7RD-1027XES-Ordenador/dp/B078ZYX4R5/ref=sr_1_1?ie=UTF8&qid=1524239679&sr=8-1";
    extractReference(str).ifPresent(System.out::println);
    String html = "<div id =\" cerberus-data-metrics \"style =\" display: none; \"data-asin =\" B078ZYX4R5 \"data-as-price = \"1479.00\" data-asin-shipping = \"0\" data-asin-currency-code = \"EUR\" data-substitute-count = \"0\" data-device-type = \"WEB\" data-display-code = \"Asin is not eligible because it has a retail offer \"> </ div>";
    extractPrice(html).ifPresent(System.out::println);
}

这将打印:

B078ZYX4R5
1479.00

更新 2:使用 URL

如果您想使用java.net.URL该类来帮助您缩小搜索范围,您可以这样做。但是您不能使用此类进行完全提取。由于您要提取的令牌位于 URL 路径中,您可以提取路径,然后应用上面解释的正则表达式进行提取。

以下是可用于缩小搜索范围的示例代码:

public static void main(String[] args) throws IOException {
    String str = "https://www.amazon.es/MSI-Titan-GT73EVR-7RD-1027XES-Ordenador/dp/B078ZYX4R5/ref=sr_1_1?ie=UTF8&qid=1524239679&sr=8-1";
    URL url = new URL(str); 
    extractReference(url.getPath() /* narrowing the search scope here */).ifPresent(System.out::println);
    String html = "<div id =\" cerberus-data-metrics \"style =\" display: none; \"data-asin =\" B078ZYX4R5 \"data-as-price = \"1479.00\" data-asin-shipping = \"0\" data-asin-currency-code = \"EUR\" data-substitute-count = \"0\" data-device-type = \"WEB\" data-display-code = \"Asin is not eligible because it has a retail offer \"> </ div>";
    extractPrice(html).ifPresent(System.out::println);
}

推荐阅读