减去部分文字
subtract part of text
我有这个代码
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 = "
中
我不想使用外部库,我知道可以通过拆分、索引和子字符串来完成
谢谢!!!
您可以使用正则表达式解决这两个任务。然而,对于第二个任务(从 HTML 中提取价格),您可以使用 JSOUP,它更适合从 HTML.
中提取内容
以下是针对您的任务的一些基于正则表达式的可能解决方案:
1。更改 URL
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();
}
那你就可以用这个方法提取reference和price了:
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
class来帮助你缩小搜索范围,你可以这样做。但是您不能使用此 class 进行完全提取。
由于您要提取的令牌位于 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);
}
我有这个代码
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 = "
我不想使用外部库,我知道可以通过拆分、索引和子字符串来完成
谢谢!!!
您可以使用正则表达式解决这两个任务。然而,对于第二个任务(从 HTML 中提取价格),您可以使用 JSOUP,它更适合从 HTML.
中提取内容以下是针对您的任务的一些基于正则表达式的可能解决方案:
1。更改 URL
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();
}
那你就可以用这个方法提取reference和price了:
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
class来帮助你缩小搜索范围,你可以这样做。但是您不能使用此 class 进行完全提取。
由于您要提取的令牌位于 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);
}