반복 크롤링 시 get_text() 결과 None인 경우를 건너뛰는 방법?
저도 같은 질문을 하려고 들어왔다가 강사님 답변을 보고 아래와 같이 해결해보았습니다. 비슷한 고민을 하신 분들께 도움이 되길 바라면서 코드 첨부해봅니다. import requests from bs4 import BeautifulSoup import openpyxl res = requests.get('http://corners.gmarket.co.kr/Bestsellers?viewType=G&groupCode=G01') soup = BeautifulSoup(res.content, 'html.parser') excel_file = openpyxl.Workbook() excel_sheet = excel_file.active excel_sheet.column_dimensions['B'].width = 100 excel_sheet.column_dimensions['C'].width = 20 excel_sheet.column_dimensions['D'].width = 20 excel_sheet.column_dimensions['E'].width = 20 excel_sheet.append(['랭킹', '상품명', '할인 전 가격', '할인률', '할인된 가격']) if res.status_code == 200 : best_list = soup.select('div.best-list') best_item = best_list[1] products = best_item.select('ul > li') for index, item in enumerate(products): name = item.select_one('a.itemname') origin_price = item.select_one('div.o-price') sale_price = item.select_one('div.s-price > strong') sale_percent = item.select_one('div.s-price em') discount_percent = 0 original_price = 0 if sale_percent == None : discount_percent = str('0%') original_price = sale_price.get_text() else : discount_percent = sale_percent.get_text() original_price = origin_price.get_text() print(str(index+1), name.get_text(), original_price, discount_percent, sale_price.get_text()) excel_sheet.append ( [str(index+1), name.get_text(), original_price, discount_percent, sale_price.get_text()]) else : print ("Error Code:", res.status_code) excel_file.save('G_market_item_list.xlsx') excel_file.close()