반응형
250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- or some instances in your deployment group are experiencing problems.
- node
- 도커
- AWS CI/CD 구축하기
- docker
- aws cicd
- aws
- java bigdecimal
- redis
- bootstrap
- chart.js
- codepipeline
- IntelliJ
- codedeploy
- chartjs
- Spring Error
- SQL
- Flux
- VPN
- codedeploy error
- kubeflow
- COALESCE
- Python
- PostgreSQL
- JavaScript
- Spring
- codebuild
- Kafka
- Jenkins
- Airflow
Archives
- Today
- Total
Small Asteroid Blog
[spring] jsoup으로 사이트 크롤링하기 본문
728x90
먼저 Jsoup 을 사용하기 위해서 build.gradle 에 jsoup 내용을 적는다.
dependencies {
// https://mvnrepository.com/artifact/org.jsoup/jsoup
implementation 'org.jsoup:jsoup:1.13.1'
}
파라메터로 keyword값을 받아와서 사용한다.
키워드값 가져오는 것은 설명하지 않겠다.
> 네이버 기사
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
@RequestMapping("jsoupcrawler")
public String jsoupCrawler(Model model, @RequestParam String keyword){
String apiUrl = "https://search.naver.com/search.naver?query="+keyword
+"&where=news&ie=utf8&sm=nws_hty";
Document doc = null;
System.out.println("apiUrl > "+apiUrl);
try {
doc = Jsoup.connect(apiUrl).get();
}catch (Exception e){
System.out.println(e);
}
ArrayList<String> al1 = new ArrayList<>();
Elements elements1 = doc.select(".news_tit");
System.out.println("elements "+elements1);
String href;
String title2;
for(Element el:elements1){
href = el.select("a").attr("href");
System.out.println("href "+href);
title2 = el.select("a").attr("title");
System.out.println("title1 "+title2);
al1.add(href);
}
System.out.println("al1 > "+al1);
return "jsoupsearch";
}
결과
구현을 위해 설명은 네이버 기사 url 로 했지만 네이버에서는 검색api 를 제공하고 있기 때문에 api 신청해서 사용하면 된다.
728x90
반응형
'백엔드 > Spring' 카테고리의 다른 글
java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true) (0) | 2021.10.19 |
---|---|
java selenium input 값 입력 및 버튼 클릭 (0) | 2021.10.18 |
[querydsl] import static com.querydsl.core.types. does not exist - gradle cache 삭제 (0) | 2021.09.23 |
[spring] querydsl 동적쿼리 (0) | 2021.09.10 |
[spring] Page 1 of 1 containing UNKNOWN instances (0) | 2021.09.06 |