반응형
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 |
Tags
- Airflow
- Spring
- PostgreSQL
- or some instances in your deployment group are experiencing problems.
- codedeploy error
- java bigdecimal
- chart.js
- Kafka
- JavaScript
- node
- aws
- IntelliJ
- Jenkins
- Python
- VPN
- docker
- codedeploy
- aws cicd
- codepipeline
- codebuild
- bootstrap
- AWS CI/CD 구축하기
- SQL
- 도커
- Flux
- redis
- chartjs
- kubeflow
- Spring Error
- COALESCE
Archives
- Today
- Total
Small Asteroid Blog
[Java] 구글 뉴스 날짜별로 크롤링하기 본문
728x90
구글에서 날짜별로 크롤링을 하고자 한다.
duration에 적혀있는 날짜는 원하는 날짜로 변경해서 사용하면된다.
구글에서 날짜기준을 적을 때 1/1/2020 이러한 포맷으로 사용해야한다.
아래에 굵은 글씨 부분의 숫자를 원하는 날짜로 변경해서 사용하면 된다.
"%2Ccd_min%3A1%2F1%2F2020%2Ccd_max%3A1%2F31%2F2020"
//google
@RequestMapping("googlejsoup")
public String googlejsoup(Model model, @RequestParam String keyword){
String apiUrl;
Document doc = null;
ArrayList<String> al1 = new ArrayList<>();
ArrayList<String> al2 = new ArrayList<>();
String href;
Elements div1;
Elements span1;
String duration;
for(int i=0;i<=400;i+=20){
doc = null;
// duration = "%2Ccd_min%3A1%2F1%2F2020%2Ccd_max%3A12%2F31%2F2020"; //20200101-20201231
duration = "%2Ccd_min%3A1%2F1%2F2020%2Ccd_max%3A1%2F31%2F2020"; //20200101-20200131
apiUrl ="https://www.google.com/search?q="+keyword+"&biw=1005&bih=842&source=lnt&tbs=cdr%3A1"+duration+"&tbm=nws"
+"&start="+i;
try {
doc = Jsoup.connect(apiUrl).get();
//기사 제목
Elements elements1 = doc.select(".WlydOe");
Elements title = doc.select("div[class=mCBkyc tNxQIb ynAwRc JIFdL JQe2Ld nDgy9d]");
for(Element el1:title){
al1.add(el1.ownText());
}
Elements content = doc.select("div[class=GI74Re nDgy9d]");
for(Element el:content){
al2.add(el.ownText());
}
int index = 0;
for(Element el:elements1){
System.out.println("title > "+al1.get(index));
System.out.println("content > "+al2.get(index));
href = el.select("a").attr("href");
System.out.println("href "+href);
div1 = el.select("div");
Element div2= div1.first();
System.out.println("div2 text >"+div2.text());
span1 = el.select("span");
Element agency= span1.first();
System.out.println("agency text >"+agency.text()+" , len > "+agency.text().length());
String main = div2.text().substring(agency.text().length(),div2.text().length()-12);
System.out.println("div main >> "+main);
String pubdate = div2.text().substring(div2.text().length()-13);
System.out.println("date >> "+pubdate);
//db저장
// Source source = new Source();
// source.setTitle(al1.get(index));
// source.setLink(href);
// source.setContent(al2.get(index));
// source.setDate(pubdate);
// source.setSearchword(keyword);
// source.setSite(agency.text());
// source.setType("뉴스");
// newsSaveService.save1(source);
index++;
}
}catch (Exception e){
System.out.println(e);
}
}
return "jsoupsearch";
}
728x90
반응형
'백엔드 > Spring' 카테고리의 다른 글
StringBuilder & System.out.println() (0) | 2021.11.24 |
---|---|
자바에서 파이썬 파일 실행하기 - processbuilder (0) | 2021.11.10 |
element is not attached to the page document (0) | 2021.11.04 |
[jquery] select 선택에 따라 두번째 select 변경하기 (0) | 2021.10.29 |
[Java] 자바 문자열에서 숫자 추출하기 (0) | 2021.10.27 |