Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

네이버 스포츠 뉴스 수집관련(인코딩) 질문 #62

Closed
xvyaward opened this issue Oct 30, 2017 · 5 comments
Closed

네이버 스포츠 뉴스 수집관련(인코딩) 질문 #62

xvyaward opened this issue Oct 30, 2017 · 5 comments

Comments

@xvyaward
Copy link

안녕하세요? 스포츠 기사 관련 크롤링을 진행하고 있는 학생입니다.
기사중에 그냥 네이버뉴스도 있고 네이버 스포츠뉴스도 있는데, 일반 네이버 뉴스에서는 잘 작동하네요.

다만 스포츠 기사는 웹코드가 조금 달라서 약간 수정을 거치고 이용해봤는데, 결정적으로 인코딩에서 에러가 생깁니다. 현재 네이버뉴스는 charset="euc-kr", 스포츠뉴스는 charset="UTF-8" 으로 되어있네요

예를들어 getContentTitle 로 얻어온 title을 출력해 보면

[[1]]
[1] "[K뒪\u0080] 쐞湲곗쓽 異뺢뎄 \u0080몴\u0080 슦利덈쿋겕 엯꽦\u0080肄붿튂吏, 7떆媛 湲곕궡 넗쓽"

[[2]]
[1] "異뺢뎄\u0080몴\u0080 肄붿튂吏, 鍮꾪뻾湲 븞뿉꽌룄 諛ㅼ깦 遺꾩꽍 "諛섎뱶떆 듅由""

이런식으로 되고 본문 내용 추출로도 넘어가지 못하네요. R언어에서 인코딩 관련하여 찾아봤지만 문제를 해결할 수 없어서.. 혹시 어느 부분을 수정하면 될지 알 수 있을까요?

@mrchypark
Copy link
Member

안녕하세요, 그럴경우 read_html(url, encoding="UTF-8")으로 해보시면 좋을것 같습니다. 감사합니다.

@xvyaward
Copy link
Author

xvyaward commented Nov 2, 2017

아직 해결을 못했습니다ㅠ
다른게 문제가 아니고 제 추측으로는
success <- function(res){
cat("Request done! Status:", res$status, "\n")
#res$content<-iconv(rawToChar(res$content),to="UTF-8")
res$content<-rawToChar(res$content)
data <<- c(data, list(res))
}
이 과정을 거치면서 문제가 발생하는 것 같은데,

[1] 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 0a 3c 68 74 6d 6c 20 6c 61 6e 67 3d 22 6b 6f 22 3e 0a 3c 68 65 61 64 3e 0a 09 3c 6d 65 74 61 20 63 68 61 72 73 65 74 3d 22 55 54 46 2d 38
[62] 22 3e 0a 09 3c 6d 65 74 61 20 68 74 74 70 2d 65 71 75 69 76 3d 22 58 2d 55 41 2d 43 6f 6d 70 61 74 69 62 6c 65 22 20 63 6f 6e 74 65 6e 74 3d 22 49 45 3d 65 64 67 65 22 3e 0a 09 3c 6d

이런 raw byte들이 변환된 후의 data를 csv파일로 저장해서 열어보면 한글만 다 깨져있었습니다. 그래서 뒤의 data를 이용하는 함수들 역시 인코딩 문제가 발생한 것 같습니다.

전체 코드를 해석해봤지만 data frame이 정확히 어떻게 구성되는지 아직 이해하는중이라, 저 부분은 잘모르겠네요.. rawTochar에서 인코딩문제를 해결할 방법이 있을까요?

아 참고로 중간에 주석처리 된 부분을 살려서 돌려봤는데,
Error: 'NA' does not exist in current working directory 의 오류가 발생하네요

@xvyaward
Copy link
Author

xvyaward commented Nov 2, 2017

해결했습니다.

Encoding(res$content) <- "UTF-8"

이 코드를 삽입하니 바로 되는군요

@mrchypark
Copy link
Member

@xvyaward 오 감사합니다. #res$content<-iconv(rawToChar(res$content),to="UTF-8") 아마 이것이 OS에 따라 동작이 다른듯 해서 주석처리 해서 그런것 같습니다. 좋은 정보 공유해주셔서 감사합니다.

@xvyaward
Copy link
Author

xvyaward commented Nov 4, 2017

혹시 가능하다면 스포츠 뉴스에서 댓글추출에 도움을 요청해도될까요..?

스포츠 뉴스 중 하나에서 크롬 개발자모드 -> network를 확인해보니 댓글수집 api가

http://apis.naver.com/commentBox/cbox/web_naver_list_jsonp.json?ticket=sports&templateId=view&pool=cbox2&_callback=jQuery1113020049989799848422_1509795433279&lang=ko&country=KR&objectId=news025%2C0002750844&categoryId=&pageSize=20&indexSize=10&groupId=&listType=OBJECT&page=1&initialize=true&userType=&useAltSort=true&replyPageSize=20&moveTo=&sort=LIKE&_=1509795433280

이런식으로 되어있더군요

그래서 getComment의
url <- paste0("https://apis.naver.com/commentBox/cbox/web_naver_list_jsonp.json?ticket=news&templateId=view_politics&pool=cbox5&lang=ko&country=KR&objectId=news", oid, "%2C", aid, "&categoryId=&pageSize=", pageSize, "&indexSize=10&groupId=&page=", page, "&initialize=true&useAltSort=true&replyPageSize=30&moveTo=&sort=", sort)

에서
ticket=newsticket=sports 로,
templateId=view_politics --> templateId=view
sort=LIKE 로 주고

url <- paste0("https://apis.naver.com/commentBox/cbox/web_naver_list_jsonp.json?ticket=sports&templateId=view&pool=cbox2&lang=ko&country=KR&objectId=news", oid, "%2C", aid, "&categoryId=&pageSize=", pageSize, "&indexSize=10&groupId=&listType=OBJECT&page=", page, "&initialize=true&userType=&useAltSort=true&replyPageSize=20&moveTo=&sort=LIKE")

이렇게 바꿔서 해봤는데 잘 안되네요.. api 건드리는 건 힘들다고 듣긴했는데 혹시 조언을 얻을 수 있을까요?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants