简体   繁体   English

我如何将JavaScript应用于存储在字符串/字符串缓冲区中的HTML

[英]How toI apply javascript to an html stored in string/stringbuffer

How can I apply javascript to an html stored in string/stringbuffer ? 如何将javascript应用于存储在string / stringbuffer中的html?
I am extracting the html of a webpage using Java 我正在使用Java提取网页的html

URL url = new URL("example.com");
InputStream is = url.openStream();
int ptr = 0;
StringBuffer buffer = new StringBuffer();
while ((ptr = is.read()) != -1) {
    buffer.append((char)ptr);
}
System.out.println(buffer);

and I want to apply javascript to the buffer to get innerHTML of some tag using document.getElementById() . 我想将JavaScript应用于缓冲区使用document.getElementById()获得某些标签的innerHTML

My purpose is to get the innerHTML of some tag inside a webpage without opening it on browser. 我的目的是获取网页内某个标签的innerHTML,而无需在浏览器中打开它。 Am I using the correct way ? 我使用的是正确的方法吗? Is there some other way to do so ? 还有其他方法吗?

You don't need JavaScript for this, not within a Java program. 您不需要JavaScript,也不需要Java程序。

You can use a DOM parser like Jsoup , and then use the methods in Jsoup that let you retrieve elements and their text. 您可以使用Jsoup之类的DOM解析器,然后使用Jsoup中的方法来检索元素及其文本。 Jsoup isn't the only library that does this, you can find others if you search. Jsoup不是唯一执行此操作的库,如果您进行搜索,则可以找到其他库。 It's one of the most popular at the moment. 它是目前最受欢迎的游戏之一。

Example using Jsoup: 使用Jsoup的示例:

Document doc = Jsoup.connect("http://example.com").get();
Element element = doc.getElementById("theIdValue");
// Read the text of the element:
String text = element.text();
// Or read the HTML of it
String html = element.html();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM