Skip to content

Instantly share code, notes, and snippets.

@Beomi
Created February 26, 2019 07:41
Show Gist options
  • Save Beomi/bc585833f6ba1e2ed6383f4d9cf03790 to your computer and use it in GitHub Desktop.
Save Beomi/bc585833f6ba1e2ed6383f4d9cf03790 to your computer and use it in GitHub Desktop.
네이버 지식인 URL 따라 질문/답변 받아오기
# coding: utf-8
import requests
from bs4 import BeautifulSoup as bs
def get_qa_from_url(url):
r = requests.get(url)
soup = bs(r.text, 'html.parser')
q_title = ' '.join(soup.select_one('h3.heading').text.split())
q_contents = ' '.join(soup.select_one('div.user_content').text.split())
answer_blocks = soup.select('div.end_a._answer._expertAnswer')
answers = []
for ans in answer_blocks:
a_title = ' '.join(ans.select_one('h3.heading').text.split())
a_contents = ' '.join(ans.select_one('div.user_content').text.split())
answers.append({
'title': a_title,
'contents': a_contents,
})
return {
'q_title': q_title,
'q_contents': q_contents,
'answers': answers,
}
# TEST
get_qa_from_url('https://m.kin.naver.com/mobile/qna/detail.nhn?d1id=7&dirId=7010106&docId=319427217&qb=64u564eo&enc=utf8%C2%A7ion=kin&rank=1&search_sort=0&spq=1')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment