새싹에서 진행하는 교육의 서브프로젝트 2번으로 클라우드자동화프로그램/모듈/라이브러리를 만드는 것이 있다.
크롤링할 사이트는 금융과 상관 없다기에(금융데이터 분석 교육이다..ㅎ) 스팀홈페이지에서 게임별로 컴퓨터사양을 크롤링하는 것을 주제로 삼았다.
신규 게임이 출시되면, 텔레그램이나 메일로 알림이 오고(자동화) 이를 확인할 수 있는 모듈을 제작하는 것이 내 목표..
사실 크롤링을 배운지는 좀 지났지만 벨로그를 시작한지 얼마 안돼서 이제서야 정리를 하기 시작했다. 데헷 ㅎㅎ
selenium
은 사용하지 않는다. requests
모듈과 BeautifulSoup
으로 진행중이다.
강사님께서도 그렇게 어려운 것이 아니라고 했고, requests
로도 충분히 구현할 수 있을 것이라 하셔서 그렇게 진행중이다.
크롤링 감을 잡기 위해서 진행했다.
import requests
from bs4 import BeautifulSoup
import json
필요한 것들 import
해주고
response
되는 url
에서 query&start=
부분을 보면 50씩 증가한다. 뒤에 count
는 50으로 증가함. (아마 게임이 50개씩 로딩되는 방식인듯)for
문에 start
를 인자로 주고 range(0, 400, 50)
으로 start
를 50씩 증가시키면서 데이터를 수집한다. 대체로 신규 출시 인기제품 카테고리는 400개가 넘지 않는 선에서 게임이 로딩되기 때문에 end
를 400으로 잡아줬다.cookie
에 이런게 숨어있더라.. requests
할때 dictionary
형태로 집어넣어주면 된다.result = []
for start in range(0, 400, 50):
URL = 'https://store.steampowered.com/search/results/?query&start='+str(start)+'&count=50&sort_by=Released_DESC&snr=1_7_7_popularnew_7&filter=popularnew&os=win&infinite=1'
res = requests.get(URL, cookies={
'Steam_Language':'koreana'
})
json_data = res.json()
soup = BeautifulSoup(json_data['results_html'], 'html.parser')
# steam = soup.select_one('#search_resultsRows')
titles = soup.select('.col.search_name.ellipsis .title')
for title in titles:
result.append(title.text)
result
이렇게 진행하면 reuslt에 다 담김.
Output exceeds the size limit. Open the full output data in a text editor
['Broken Pieces',
'Jack Move',
'Steelrising',
'NBA 2K23',
'Adorable Witch 4 :Lust',
'Temtem',
'레일바운드 - Railbound',
'RUNGORE: Beginner Experience',
'죠죠의 기묘한 모험 올 스타 배틀 R',
'짱구는 못말려 나와 박사의 여름 방학 ~끝나지 않는 7일간의 여행~',
'a pet shop after dark',
'Farmer Against Potatoes Idle',
'SD건담 배틀 얼라이언스',
'트리플 판타지',
'Rune Teller',
'Thymesia',
'Backpack Hero',
'Marvel’s Spider-Man Remastered',
'Cult of the Lamb',
'바둑',
'Farthest Frontier',
'Two Point Campus',
'디지몬 서바이브',
'영웅전설 여의 궤적',
'Stray',
...
"Sid Meier's Civilization® V",
'Left 4 Dead 2',
'Synergy',
'Team Fortress 2',
"Garry's Mod"]
'https://store.steampowered.com/app/'
+app id
로 각각의 게임들의 페이지에 접속할 수 있다.# a태그의 appid들을 수집
id_list = []
for start in range(0, 400, 50):
URL = 'https://store.steampowered.com/search/results/?query&start='+str(start)+'&count=50&sort_by=Released_DESC&snr=1_7_7_popularnew_7&filter=popularnew&os=win&infinite=1'
res = requests.get(URL, cookies={
'Steam_Language':'koreana'
})
json_data = res.json()
soup = BeautifulSoup(json_data['results_html'], 'html.parser')
appids = soup.select('a')
for appid in appids:
idtag = appid.attrs['data-ds-appid']
id_list.append(idtag)
id_list
Output exceeds the size limit. Open the full output data in a text editor
['1283400',
'745920',
'1967510',
'1918540',
'2113050',
'1372110',
'2061250',
'1934800',
'1777620',
'1535560',
'824550',
'1815630',
'1933450',
'1944360',
'1343240',
'1970580',
'1817070',
'1313140',
'1862520',
'1044720',
'1649080',
'1986760',
'871980',
'1811950',
'1332010',
...
'4560,9340,20540,228200',
'500',
'17520',
'440',
'4000']
역시 잘 출력됨. '4560,9340,20540,228200'
이런 app id
가 있어서 찾아보니 패키지로 묶어파는 것들은 저렇게 여러개가 줄지어서 나오더라. 해당 게임 페이지에 접속하는데 문제는 없다.
솔직히 이정도 했으면 거의 다한거나 마찬가지인데 나에게 닥친 문제가.. 데이터 가공을 어떻게 하느냐
이다. 데이터를 긁어오는건 쉽다. 근데 게임별로 사양들을 정리하기가 어렵다..
다음은 이 문제들을 해결하는 코드들을 작성해 보겠다. (해결할 수 있다면)