0
특정 Google 검색에서 제공되는 모든 링크와 미리보기 이미지를 가져오고 싶습니다. 여기 내 코드가있다. Google 구문 분석 Google 결과
package com.esocial.util;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class ListLinks {
public static void main(String[] args) throws IOException {
String url = "https://www.google.co.in/webhp?sourceid=chrome-instant&rlz=1C1CHWA_enIN609IN609&ion=1&espv=2&ie=UTF-8#q=thermodynamics%20cbse";
System.out.println("Fetching : "+url+"\n\n");
Document doc = Jsoup.connect(url).userAgent("Mozilla").get();
Elements div = doc.select("div.srg");
for(Element di : div)
{
Elements lists = di.select("li.g");
for(Element list : lists)
{
Element anc = list.select("a").first();
Element img = list.select("img").first();
System.out.println("\nLink : "+anc.attr("href")+"\nImage Link : "+img.attr("src")+"\n------------------------------------------\n");
}
}
}
}
는하지만이 코드가 제대로 실행되지 않고 결과를 표시하지 않습니다. 나는 그 문제가 무엇인지 이해하지 못한다.
정확히 추출하려고하는 대상은 무엇입니까? 또한'div.srg'는 무엇입니까 (나는 그런 클래스와 div를 찾을 수 없습니다)? 아마 당신이 그것을 분석하기 전에'doc'의 내용을 인쇄하는 것을 고려해보십시오. 또한 Jsoup는 JavaScript 에뮬레이터가 아니라 간단한 파서이므로 JavaScript로 생성 된 내용이 있으면 파싱 할 수 없습니다. – Pshemo
다양한 div에 내장되어 있습니다. 이것들은 목록 안에있는 div이고, 그 목록 항목은 앵커 태그와 img 태그입니다. jsoup에서 코딩을 시작한 이래로이 잘못된 방식으로 접근하고있을 수 있습니다. 앵커 링크와 해당 img 링크를 가져 와서 링크를위한 CSV 파일을 만들면 좋을 것입니다. –
'System.out.println (doc)'을 통해'doc'의 내용을 출력 할 때'srg' 클래스로'div'를 찾을 수 있습니까? – Pshemo