크롤링 파이썬 코드
import requests
import json
singer = input("검색하고 싶은 가수: ")
url = "https://www.melon.com/search/keyword/index.json"
params = {'jscallback' : "jQuery19105357803934720518_1603168193882",
'query' : singer}
headers = {"Referer" : "http://www.melon.com/index.htm",
"User-Agent" : ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36\
(KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36")}
response = requests.get(url, headers = headers, params = params)
response = response.text
json_string = response.replace(params['jscallback'] + '(', '').replace(');', '')
result_dict = json.loads(json_string)
print(type(result_dict)) # <class 'dcit'>
print(result_dict.keys(), end = '\n\n')
for song in result_dict['SONGCONTENTS']:
print(song['SONGNAME'])
블랙핑크를 검색한 결과,
..
import requests
import json
def load_song_list(singer):
url = "https://www.melon.com/search/keyword/index.json"
params = {
'jscallback' : "jQuery19105357803934720518_1603168193882",
'query' : singer}
headers = {
'Referer' : "http://www.melon.com/index.htm",
"User-Agent" : ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36\
(KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36")}
response = requests.get(url, headers = headers, params = params)
response = response.text
json_string = response.replace(params['jscallback'] + '(', '').replace(');', '')
result_dict = json.loads(json_string)
song_list = []
for song in result_dict['SONGCONTENTS']:
song_list.append(song['SONGNAME'])
return song_list
singer = input("검색하고 싶은 가수: ")
result = load_song_list(singer)
print(result)
함수에 코드를 넣고 결과 출력!
너무 멋집니다!!!!!!