
import requests
from bs4 import BeautifulSoup
req = requests.get('https://www.krcert.or.kr/kr/bbs/list.do?bbsId=B0000302&menuNo=205023')
scrap = BeautifulSoup(req.content, 'html.parser')
table = scrap.find('table')
tbody = table.find('tbody')
trs = tbody.find('tr')
tds = trs.find_all('td')
num = tds[0].text
title = tds[1].text
title2 = title.replace("\n", "")
severity = tds[2].text
cvss = tds[3].text
vuln_dic = {'num':num, 'title':title2, 'severity':severity, 'cvss':cvss}
print(vuln_dic)
trs = tbody.find_all("tr")

<td> 내용 </td> 하나가 첫번째 내용이다.<td>가 여러개이고 이를 find_all로 찾아 필요한 내용만 추출할 수 있다.

<td>들을 리스트 순서대로 추출할 것이다.for tr in trs:
tds = tr.find_all("td")

num = tds[0].text
title = tds[1].text
severity = tds[2].text
cvss = tds[3].text

dic = {'num':num, 'title':title, 'severity':severity, 'cvss':cvss}
dictionary는 key:value 형태로 "A는 B다"라고 생각하면 된다.

retitle = title.replace("\r", "").replace("\t", "").replace("\n", "")

여기까지 왔다면 거의 다 끝났다고 볼 수 있다.
vuln = []
vuln.append(dic)
vuln 변수는 꼭 for 문 전에 지정해줘야 한다. for문 안에 vuln 변수가 있다면 계속해서 리셋되어 마지막 정보만 vuln 변수에 저장될 것이다.
vuln = []
for tr in trs:
tds = tr.find_all("td")
num = tds[0].text
title = tds[1].text
retitle = title.replace("\r", "").replace("\t", "").replace("\n", "")
severity = tds[2].text
cvss = tds[3].text
dic = {'num':num, 'title':retitle, 'severity':severity, 'cvss':cvss}
vuln.append(dic)

jsondump = json.dumps(vuln, indent=4)
print(jsondump)
indent는 보통 4를 이용한다. 구분자(,)에 따라 줄바꿈을 해주어 이쁘게 작성된다.
with open('vuln.json', "w", encoding="utf-8") as make_file:
with ~ as 변수: 는 with 하위의 동작이 끝나면 닫는다.
json.dump(vuln, make_file, ensure_ascii=False, indent=4)
import requests
from bs4 import BeautifulSoup
import json
req = requests.get('https://www.krcert.or.kr/kr/bbs/list.do?bbsId=B0000302&menuNo=205023')
scrap = BeautifulSoup(req.content, 'html.parser')
table = scrap.find('table')
tbody = table.find('tbody')
trs = tbody.find_all('tr')
vuln = []
for tr in trs:
tds = tr.find_all("td")
num = tds[0].text
title = tds[1].text
retitle = title.replace("\r", "").replace("\t", "").replace("\n", "")
severity = tds[2].text
cvss = tds[3].text
dic = {'num':num, 'title':retitle, 'severity':severity, 'cvss':cvss}
vuln.append(dic)
with open('vuln.json', "w", encoding="utf-8") as make_file:
json.dump(vuln, make_file, ensure_ascii=False, indent=4)
