gpt4 book ai didi

java - 当我使用 BufferedReader 获取 HTML 时,我需要的部分不存在

转载 作者:行者123 更新时间:2023-12-02 04:13:13 25 4
gpt4 key购买 nike

因此,我放置了这样的代码,以从网站中的某个标记获取值:

    try {

URL url = new URL("google.com");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

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

inputLine = in.readLine();
}
in.close();


} catch (IOException e) {

e.printStackTrace();

}

所以说我需要它来找到“Pizza”,但只弹出一些代码,所以我无法访问该部分,有没有办法可以打印整个 HTML (使用 BufferReader 并且没有像 Jsoup 这样的额外导入),并且然后检查一下?

最佳答案

  URL url = new URL("http://www.google.com");
URLConnection uc = url.openConnection();

InputStreamReader input = new InputStreamReader(uc.getInputStream());
BufferedReader in = new BufferedReader(input);
String inputLine;

FileWriter outFile = new FileWriter("orhancan");
PrintWriter out = new PrintWriter(outFile);

while ((inputLine = in.readLine()) != null) {
out.println(inputLine);
}

in.close();
out.close();

File fXmlFile = new File("orhancan");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);


NodeList prelist = doc.getElementsByTagName("body");
System.out.println(prelist.getLength());

有一种更简单的方法可以做到这一点。我建议使用 JSoup。使用 JSoup 你可以做类似的事情。 json 文档 doc = Jsoup.connect("http://en.wikipedia.org/ ").get(); 元素 newsHeadlines = doc.select("#mp-itn b a");或者如果你想要 body :

Elements body = doc.select("body");

或者如果您想要所有链接:

Elements links = doc.select("body a");

关于java - 当我使用 BufferedReader 获取 HTML 时,我需要的部分不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33586962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com