Java 需要一个特定的弦部分

Java 需要一个特定的弦部分,java,Java,我正在做一个应用程序,应该采取整个网站的html文本,并把它放入字符串。 然后我想使用System.out.println来显示该字符串的某个片段。我的代码 import java.net.*; import java.io.*; public class URLConnectionReader { public static void main(String[] args) throws Exception { URL oracle = new URL("www.e

我正在做一个应用程序,应该采取整个网站的html文本,并把它放入字符串。 然后我想使用System.out.println来显示该字符串的某个片段。我的代码

import java.net.*;
import java.io.*;

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {

        URL oracle = new URL("www.example-blahblahblah.com");
        BufferedReader in = new BufferedReader(
        new InputStreamReader(oracle.openStream()));

        String inputLine;
        while ((inputLine = in.readLine()) != null)

       System.out.println(inputLine.substring(inputLine.indexOf("<section class=\"horoscope-content\"><p>")+1, inputLine.lastIndexOf("</p")));

        in.close();
    }
}
import java.net.*;
导入java.io.*;
公共类URLConnectionReader{
公共静态void main(字符串[]args)引发异常{
URL oracle=新URL(“www.example-blahblahblah.com”);
BufferedReader in=新的BufferedReader(
新的InputStreamReader(oracle.openStream());
字符串输入线;
而((inputLine=in.readLine())!=null)

System.out.println(inputLine.substring(inputLine.indexOf(“”)+1,inputLine.lastIndexOf(“”)您的代码每次检查while语句中的条件时都会重新分配inputLine,这取决于HTML,您可能希望在查找标记部分之前读取整个文件。
除非您确信HTML包含这些文本部分,否则当它不存在时,您仍然会遇到异常。
您也只在开始时将索引增加了1,如果您不希望开始文本输出,则必须增加开始部分的长度

您可以尝试以下方法:

StringBuilder html = new StringBuilder(); //holds all of the html we read
String inputLine;
while ((inputLine = in.readLine()) != null) { //read line by line
  html.append(inputLine); //add line to html
}
inputLine = html.toString(); //get 
String startText = "<section class=\"horoscope-content\"><p>"; //starting tag
int start = inputLine.indexOf(startText);
int end = inputLine.lastIndexOf("</p"); //might want to use something like inputLine.indexOf("</p>", start); if there are multiple sections on the page
if(start >= 0 && end >= 0) { //make sure we found a section
  System.out.println(inputLine.substring(start+startText.length(), end)); //print everything between the start and end tags (excluding the text in the start tag)
} else {
  System.out.println("section not found"); //do something else since we didn't find the tags
}
StringBuilder html=new StringBuilder();//保存我们读取的所有html
字符串输入线;
而((inputLine=in.readLine())!=null){//逐行读取
append(inputLine);//将行添加到html
}
inputLine=html.toString();//获取
字符串startText=“”;//起始标记
int start=inputLine.indexOf(startText);

int end=inputLine.lastIndexOf(“您应该使用一个更宽容的正则表达式,而不是
indexOf
,以便在输入的微小修改方面更稳定:

Pattern pattern = Pattern.compile("<section\\s+class\\s*=\\s*\"horoscope-content\"\\s*>\\s*<p>(.*?)</p>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
    System.out.println(matcher.group());
    System.out.println("Text in paragraph: " + matcher.group(1));
}
Pattern=Pattern.compile(“\\s*(.*?

”,Pattern.DOTALL); 匹配器匹配器=模式匹配器(线); if(matcher.find()){ System.out.println(matcher.group()); System.out.println(“段落中的文本:“+matcher.group(1)); }

对于换行符和其他空白字符,这是可以容忍的。

indexOf
lastIndexOf
如果未找到字符,则返回
-1
。您对indexOf的第二次调用返回-1,表示未找到子字符串。打印整个字符串以查看其内容。我怀疑您正在查找的文本对子字符串的调用中的ng被分成多行(因此分成不同的字符串)。在将短语的位置用作索引之前,应使用
contains()
检查短语。这意味着匹配的字符串(“”)。如果且仅当找到字符串时,它将返回该字符串的索引。否则它将始终返回-1。由于未找到该字符串,您是否打算在末尾包含“”?添加到@HunterMcMillen的注释中;第一步是验证您正在查找的行是否确实存在于从服务器获得的响应中。
Pattern pattern = Pattern.compile("<section\\s+class\\s*=\\s*\"horoscope-content\"\\s*>\\s*<p>(.*?)</p>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(line);
if (matcher.find()) {
    System.out.println(matcher.group());
    System.out.println("Text in paragraph: " + matcher.group(1));
}