파이썬 딕셔너리를 CSV파일로 저장하기
조회수 3591회
import csv
def getContents(x):
html = urllib.request.urlopen(x)
bsObj = bs4.BeautifulSoup(html, "html.parser")
return bsObj.find("div", {"class":"user_content"}).text
contentsHolder = {x:getContents(x) for x in informationurls}
def toCSV(crawling_data):
file = open('html_crawling_data2.csv', 'w', encoding='utf-8', newline='')
csvfile = csv.writer(file)
for row in crawling_data :
csvfile.writerow(row)
file.close()
toCSV(contentsHolder)
덕분에 여기까지 오게되었습니다 이제 정말 저장만하면되는데!!
사실 찾는 클래스의 user_contents가 없는 html주소들이 있어서 HTTP에러가 몇개 정도 납니다 csv파일로 저장되지 않는 이유가 그것 때문이라고 생각합니다. 그래서 예외처리를 해서 오류가 뜨더라도 다음거를 바로바로 넘어가서 딕셔너리에 넣고 저장해야하는데 try except 구문을 사용해봤는데 똑같더라고여;;;; 무언가 방법이 있을까요?
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력