减去部分文字

时间:2018-04-26 07:11:55

标签: java split substring indexof

我有这段代码

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();
}

}

我想从网址中删除 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 的价格,它标签中包含&#34; data-as-price =&#34;

我不想使用外部库,我知道可以使用split,index和substring来完成

感谢!!!!

1 个答案:

答案 0 :(得分:0)

您可以使用正则表达式解决这两个任务。然而,对于第二项任务(从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);
}