Java Read Html

[b]Need Jar:[/b]
jsoup-1.8.1.jar



public static void main(String[] args) {
String content="";
try {
content = executeGet("qq.com","t.qq.com","pgv_pvi=2633264128; RK=AWkaEwm4WM; ptcz=b94a87c80a0a85ceec47cd63566d582d7110bb329e378f2ef066185e9b957333; pt2gguin=o0002442254; ts_refer=url.cn/sorry; wbilang_10000=zh_TW; mb_reg_quick=1; wb_regf=%3B0%3B%3Bapi.t.qq.com%3B0; pgv_info=ssid=s1646546122; ts_last=t.qq.com/snow13000521; pgv_pvid=6340134792; o_cookie=2442254; ts_uid=9369757478");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
org.jsoup.nodes.Document doc = (org.jsoup.nodes.Document) Jsoup.parse(content);

org.jsoup.nodes.Element element = doc.getElementById("mainWrapper");
org.jsoup.nodes.Document doc2 = (org.jsoup.nodes.Document) Jsoup.parse(element.getElementsByClass("avatar").toString());
Elements elements = doc2.select("a[href]");
String qqUrl = "";
for(org.jsoup.nodes.Element ele : elements){
qqUrl = ele.attr("href");
}

//To get region
try {
content = executeGet("qq.com","t.qq.com","pgv_pvi=2633264128; RK=AWkaEwm4WM; ptcz=b94a87c80a0a85ceec47cd63566d582d7110bb329e378f2ef066185e9b957333; pt2gguin=o0002442254; ts_refer=url.cn/sorry; wbilang_10000=zh_TW; mb_reg_quick=1; wb_regf=%3B0%3B%3Bapi.t.qq.com%3B0; pgv_info=ssid=s1646546122; ts_last=t.qq.com/snow13000521; pgv_pvid=6340134792; o_cookie=2442254; ts_uid=9369757478");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(content);
if(content.length()>0){
org.jsoup.nodes.Document docAuthor = (org.jsoup.nodes.Document) Jsoup.parse(content);
Elements elementAuthors = docAuthor.getElementsByClass("ico_location");
if(elementAuthors.size()>0){
org.jsoup.nodes.Element elementAuthor = elementAuthors.get(0).nextElementSibling();
System.err.println("==QQ Region==== "+elementAuthor.text());
}
}
}
Java 中,可以通过使用内置的 java.net 包来读取 HTML 文档。下面是一个简单的代码示例: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; public class ReadHTML { public static void main(String[] args) { try { URL url = new URL("https://www.example.com"); // 替换为要读取的网页地址 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); } catch (Exception e) { System.out.println(e.getMessage()); } } } ``` 这段代码通过打开一个 URL 连接,然后使用 BufferedReader 对象来读取网页内容。最后,它将每一行内容打印到控制台。 如果你想要写入 HTML 文档,可以使用 Java 的 FileWriter 或者 BufferedWriter 类。具体实现可以参考以下代码示例: ```java import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class WriteHTML { public static void main(String[] args) { try { String content = "<html><body><h1>Hello, world!</h1></body></html>"; // 要写入的内容 File file = new File("output.html"); // 文件名 FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } } } ``` 这段代码将一个包含 HTML 代码的字符串写入到一个名为 `output.html` 的文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值