제목 하나하나를 읽고, html 태그가 있으면 떼는 기능을 담아줄 것이다.
html 태그가 있으면 떼주는 기능을 하는 'delete_tag'함수를 생성한다.
def delete_tag(input_str):
input_str = input_str.replace("<b>", "")
input_str = input_str.replace("</b>", "")
return input_str
태그가 열리고 닫히기 때문에 태그 제거하는 코드를 두 번 써준다.
delete_tag(태그 제거하는 함수)를 적용하기 위해 위에 코드를 복사해 온다.
import pandas as pd
def get_fields(json_data):
title = [delete_tag(each["title"]) for each in json_data["items"]]
link = [each["link"] for each in json_data["items"]]
lprice = [each["lprice"] for each in json_data["items"]]
mall_name = [each["mallName"] for each in json_data["items"]]
result_pd = pd.DataFrame({
"title": title,
"link": link,
"lprice":lprice,
"mall":mall_name,
}, columns=["title", "lprice", "link", "mall"])
return result_pd
코드 설명:
get_fields(one_result)
html 태그가 제거된 것을 볼 수 있다.
url = gen_search_url("shop", "몰스킨", 1, 10)
json_result = get_result_onpage(url)
pd_result = get_fields(json_result)
코드 설명:
pd_result