내가 만들고자 하는 프로젝트는
학교 홈페이지 내의 취업공고란을 크롤링해 앱으로 옮겨담고,
사용자가 등록한 키워드에 대해서 푸시알림을 보내는 것이다.
이를 위해 학교 홈페이지를 크롤링해보자.
딱히 로그인이 필요 없는 페이지를 Jsoup을 이용해 크롤링해보았다.
생각보다 간단하다.
>> 전체 코드
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employ);
//액션바 숨김
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
//취업공지란 리스트뷰
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
//취업공지 crawling
final Bundle bundle = new Bundle();
new Thread(){
@Override
public void run() {
Document doc = null;
try {
doc = Jsoup.connect(url).get();
Elements contents = doc.select(".artclLinkView strong");
Elements urls = doc.select("._artclTdTitle a");
for(Element buff : contents){
String save = buff.text();
list.add(save);
}
for(Element e : urls){
//noticeCrawlingArrayList.add(e.text());
UrlArrayList.add(e.attr("href"));
System.out.println(e.attr("href"));
}
for(String strBuff : titles){
title += strBuff + "\n";
}
bundle.putString("title", title);
Message msg = handler.obtainMessage();
msg.setData(bundle);
handler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a_parent, View a_view, int a_position, long a_id) {
String employTitle = (String) a_parent.getAdapter().getItem(a_position);
String employUrl = UrlArrayList.get((int)a_id);
Intent intent = new Intent(getApplicationContext(), EmployPage.class);
intent.putExtra("url", employUrl);
intent.putExtra("title", employTitle);
startActivity(intent);
}
});
}
//oncreate 끝
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
Bundle bundle = msg.getData();
textView.setText(bundle.getString("title"));
}
};
1. XML - 취업공고란을 띄울 리스트뷰를 생성한다.
2. 웹페이지에서 각 공고의 제목과 url를 뽑아낸다.
쓰레드에서 크롤링하고, 핸들러로 뷰에다가 뿌려주는 형식으로 해주었다.
개발자 도구를 켜, 뽑아내고자 하는 (나의 경우에는 공지제목들) 의 태그들을 유심히 살펴본다.
상위 태그의 class 이름이나 id를 알아낸 뒤, 뽑아내고자 하는 해당 태그를 써주면 된다.
위의 경우에서는 '2022학년도 재외국민과 외국인 특별전형 진행을 위한 출입 통제 안내'가 <strong> 태그에 있고,
이의 상위태그 <a> 태그의 class이름이 "artclLinkView"이다.
그럼 element에 제목들이 좌라락 들어가게 될 것이다.
스트링에 제목들을 넣어주고, add() 함수로 list에 넣어주면 된다.
>> 결과
왼쪽이 안드로이드 스튜디오 크롤링앱 / 오른쪽이 학교 홈페이지 공지들
이제 리스트뷰를 클릭했을 때 화면내용을 출력해주자.
앞 과정에서, 공지들의 제목 뿐만 아니라 url들도 크롤링해주었었다.
UrlArrayList에 url들을 차례차례 넣어주고,
클릭 시 나오는 액티비티(EmployPage.class) 에다가 intent로 제목과 url들을 넘겨주었다.
그 다음 EmployPage.class 에서 intent로 데이터를 받아준다.
그리고 앞 과정에서 나왔던 그래도 화면내용들을 크롤링해 출력해주면 된다.
여기서 유의점!
리스트뷰에선 해당되지않았었지만, 텍스트뷰나 그 외 뷰들로 출력을 해주고자 할 때에는 반드시 핸들러를 사용한다.
>> 결과
링크 이동 버튼 또한 만들어주었다. 버튼 클릭 시 크롬으로 연결되어 해당 공고 페이지가 나타난다.
'프로젝트' 카테고리의 다른 글
[안드로이드 스튜디오] 커스텀 리스트뷰 검색기능 (0) | 2021.08.24 |
---|---|
[Android Studio] 파이어베이스를 활용한 키워드 구독 / FCM (Firebase Cloud Messaging) (0) | 2021.07.15 |
학교 졸업요건+공지사항 알리미 앱 만들기_구상편 (0) | 2021.07.07 |
[Star UML] '음료 자판기' 시퀀스 다이어그램 작성 실습 (0) | 2021.05.04 |
웹과 정보_웹사이트_옆으로 넘어가는 페이지 만들기 (0) | 2021.03.09 |