Java 파충류:dom 메서드를 사용하여 Document 객체 스트리밍
76094 단어 JSoup
https://wall.alphacoders.com/featured.php?lang=Chinese
주요 단계:
String html = "https://wall.alphacoders.com/featured.php?lang=Chinese";
Document doc = Jsoup.connect(html).get();
컨텐트가 너무 길면 더 이상 표시되지 않습니다.
우리는 이 부분을 예로 들 수 있다.
<ul class="nav nav-pills">
<li><a href="https://alphacoders.com/site/about-us" rel="nofollow">About Usa>li>
<li><a href="https://alphacoders.com/site/faq" rel="nofollow">FAQa>li>
<li><a href="https://alphacoders.com/site/privacy" rel="nofollow">Privacy Policya>li>
<li><a href="https://alphacoders.com/site/tos" rel="nofollow">Terms Of Servicea>li>
<li><a href="https://alphacoders.com/site/acceptable_use" rel="nofollow">Acceptable Usea>li>
<li><a href="https://alphacoders.com/site/etiquette" rel="nofollow">Etiquettea>li>
<li><a href="https://alphacoders.com/site/advertising" rel="nofollow">Advertise With Usa>li>
<li><a id="change_consent">Change Consenta>li>
ul>
Elements elements = doc.getElementsByTag("ul");
출력은 다음과 같습니다.
<ul class="nav navbar-nav center">
<li> <a title="Submit Wallpapers" href="https://alphacoders.com/site/submit-wallpaper"><i class="el el-circle-arrow-up">i> a> li>
<li> <a href="https://alphacoders.com/contest"><i class="el el-gift">i> a> li>
ul>
<ul class="nav navbar-nav navbar-right center">
<li> <a href="language.php?lang=Chinese"> <img src="https://static.alphacoders.com/wa/Chinese-flag.png" alt="Chinese-flag"> a> li>
<li> <a rel="nofollow" href="https://alphacoders.com/users/login"><i class="el el-user">i> a> li>
<li> <a href="https://alphacoders.com/users/register"><i class="el el-edit">i> a> li>
ul>
<ul class="pagination">
<li class="active"><a id="prev_page" href="#">< a>li>
<li class="active"><a>1a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=2">2a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=3">3a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=4">4a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=5">5a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=6">6a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=7">7a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=8">8a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=9">9a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=10">10a>li>
<li><a>...a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=319">319a>li>
<li><a id="next_page" href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=2"> >a>li>
ul>
<ul class="pagination">
<li class="active"><a href="#">< a>li>
<li class="active"><a>1a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=2">2a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=3">3a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=4">4a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=5">5a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=6">6a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=7">7a>li>
<li><a>...a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=319">319a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=2"> >a>li>
ul>
<ul class="pagination">
<li class="active"><a href="#"><< a>li>
<li class="active"><a href="#">< a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=2"> >a>li>
<li><a title=" (319)" href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=319"> >>a>li>
ul>
<ul class="pagination">
<li class="active"><a href="#">1a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=2">2a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=3">3a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=4">4a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=5">5a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=6">6a>li>
<li><a href="https://wall.alphacoders.com/featured.php?lang=Chinese&page=7">7a>li>
ul>
<ul class="nav nav-pills">
<li><a href="https://alphacoders.com/site/about-us" rel="nofollow">About Usa>li>
<li><a href="https://alphacoders.com/site/faq" rel="nofollow">FAQa>li>
<li><a href="https://alphacoders.com/site/privacy" rel="nofollow">Privacy Policya>li>
<li><a href="https://alphacoders.com/site/tos" rel="nofollow">Terms Of Servicea>li>
<li><a href="https://alphacoders.com/site/acceptable_use" rel="nofollow">Acceptable Usea>li>
<li><a href="https://alphacoders.com/site/etiquette" rel="nofollow">Etiquettea>li>
<li><a href="https://alphacoders.com/site/advertising" rel="nofollow">Advertise With Usa>li>
<li><a id="change_consent">Change Consenta>li>
ul>
Elements elements = doc.getElementsByTag("ul");
//System.out.println(elements);
Element tempElement = null;
for(Element element : elements) {
if (element.className().equals("nav nav-pills")) {
tempElement = element;
//System.out.println(element.className());
break;
}
}
Elements li = tempElement.getElementsByTag("li");
for(Element element : li) {
Elements element2 = element.getElementsByTag("a");
for(Element element3 : element2) {
String hrefString = element3.attr("href");
String relString = element3.attr("rel");
if(hrefString != "" && relString != "") {
System.out.println("href=" + hrefString + " " + "rel=" + relString);
}
}
}
최종 결과:
href=https://alphacoders.com/site/about-us rel=nofollow
href=https://alphacoders.com/site/faq rel=nofollow
href=https://alphacoders.com/site/privacy rel=nofollow
href=https://alphacoders.com/site/tos rel=nofollow
href=https://alphacoders.com/site/acceptable_use rel=nofollow
href=https://alphacoders.com/site/etiquette rel=nofollow
href=https://alphacoders.com/site/advertising rel=nofollow
전체 코드:
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import org.jsoup.Jsoup;
/**
* @ClassName: Jsoup_Test
* @description:
* @author: KI
* @Date: 2020 8 17 8:15:14
*/
public class Jsoup_Test {
public static void main(String[] args) throws IOException {
// TODO
String html = "https://wall.alphacoders.com/featured.php?lang=Chinese";
Document doc = Jsoup.connect(html).get();
System.out.println(doc);
Elements elements = doc.getElementsByTag("ul");
//System.out.println(elements);
Element tempElement = null;
for(Element element : elements) {
if (element.className().equals("nav nav-pills")) {
tempElement = element;
//System.out.println(element.className());
break;
}
}
System.out.println(tempElement);
Elements li = tempElement.getElementsByTag("li");
for(Element element : li) {
Elements element2 = element.getElementsByTag("a");
for(Element element3 : element2) {
String hrefString = element3.attr("href");
String relString = element3.attr("rel");
if(hrefString != "" && relString != "") {
System.out.println("href=" + hrefString + " " + "rel=" + relString);
}
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Java 파충류:dom 메서드를 사용하여 Document 객체 스트리밍먼저 웹 주소를 입력하십시오. 주요 단계: Jsoup의connect 방법을 이용하여 Document 대상을 획득 컨텐트가 너무 길면 더 이상 표시되지 않습니다. 우리는 이 부분을 예로 들 수 있다. 우리 먼저 모든u...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.