Java를 사용 하여이 그림의 링크를 얻고 싶습니다. 이미지는 아래에 있습니다. 해당 웹 페이지에는 링크가 거의 없습니다. 이 코드를 stackoverflow에서 찾았지만 사용 방법을 이해하지 못합니다.
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class weber{
public static void main(String[] args)throws Exception{
String url = "http://www.skyovnis.com/category/ufology/";
Document doc = Jsoup.connect(url).get();
/*String question = doc.select("#site-inner").text();
System.out.println("Question: " + question);*/
Elements anser = doc.select("#container .entry-title a");
for (Element anse : anser){
System.out.println("Answer: " + anse.text());
}
}
}
코드는 내가 찾은 원본에서 편집되었습니다. 도와주세요.
URL의 경우 다음 코드가 제대로 작동합니다.
public static void main(String[] args) {
Document doc;
try {
// need http protocol
doc = Jsoup.connect("http://www.skyovnis.com/category/ufology/").userAgent("Mozilla").get();
// get page title
String title = doc.title();
System.out.println("title : " + title);
// get all links (this is what you want)
Elements links = doc.select("a[href]");
for (Element link : links) {
// get the value from href attribute
System.out.println("\nlink : " + link.attr("href"));
System.out.println("text : " + link.text());
}
} catch (IOException e) {
e.printStackTrace();
}
}
출력은
title : Ufology
link : http://www.shop.skyovnis.com/
text : Shop
link : http://www.shop.skyovnis.com/product-category/books/
text : Books
다음 코드는 텍스트별로 링크를 필터링합니다.
for (Element link : links) {
if(link.text().contains("Arecibo Message"))//find the link with some texts
{
System.out.println("here is the element you need");
System.out.println("\nlink : " + link.attr("href"));
System.out.println("text : " + link.text());
}
}
HTTP 403 오류 메시지를 피하기 위해 Jsoup에서 "userAgent"를 지정하는 것이 좋습니다.
문서 문서 = Jsoup.connect ( " http://anyurl.com ") .userAgent ( "Mozilla"). get ();
"온나 말리 마법사 유투 카마 칼라."
참조 :
https://www.mkyong.com/java/jsoup-html-parser-hello-world-examples/
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다