โ๐ป ๋ฐฐ์ด์
CSRFToken ์ ๋ํด ์ ๋ชฐ๋์๋๋ฐ, ์ ์ฌ์ฉํ๋์ง, ์ด๋ป๊ฒ ์ฒ๋ฆฌํด ์ฃผ์ด์ผ ํ๋์ง ๋ฐฐ์ธ ์ ์์๋ค.๊ทธ๋ฆฌ๊ณ GS25 ํธ์์ ๋งค์ฅ์กฐํ ํฌ๋กค๋ง์ ์งํํ๋๋ฐ, ๋ฐ์ดํฐ์ ์ ๊ทผํ๋ ๋ฐฉ์๋ค์ด ๊ฝค ์ด๋ ค์์ ๋ ๊ณต๋ถํด์ผ ํ ๊ฒ ๊ฐ๋ค...
๐ ๋ชฉ์ฐจ
1. CSRFToken
2. ์ ๊ตญ GS25 ํธ์์ ๋งค์ฅ ์ง์ญ ์กฐํํ๊ธฐ
3. ์ ๊ตญ GS25 ํธ์์ ๋งค์ฅ ์๋น์ค ํ์ธํ๊ธฐ
๋ค์ด๊ฐ๋ฉฐ
GS25 ํธ์์ ๋งค์ฅ ๊ฒ์ ์น ํ์ด์ง ํฌ๋กค๋ง ๊ธฐ๋ฐ์ผ๋ก ๊ธ์ ์์ฑํ์ต๋๋ค. ์ฐธ๊ณ ๋ถํ๋๋ฆฝ๋๋ค :)
CSRFToken
์ด๋ฒ์๋ GS25 ํธ์์ ์ ๋ํ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ๊ฒ์ด๋ค.
GS25 ๊ฐ์ ๊ฒฝ์ฐ, URL ์ ํ์ธํด๋ณด๋ฉด CSRFToken ์ด๋ผ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
'CSRF' ๋ผ๋ ๊ฑด ์ฌ์ด๋ฒ ๊ณต๊ฒฉ ์ค ํ๋์ธ๋ฐ, ์ด๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด์ CSRF ํ ํฐ์ด๋ผ๋ ๊ฒ์ ์ฌ์ฉ์์๊ฒ ๋ฐ์ ์๋ชป๋ ์์ฒญ์ธ์ง ์๋์ง ํ์ธํ๊ธฐ ์ํด ์กด์ฌํ๋ ๊ฒ์ด๋ค.
๊ทธ๋์, ํฌ๋กค๋ง ํ ๋ URL ์์ CSRFToken ์ด๋ผ๋ ๊ฒ ๋ณด์ด๋ฉด ์ด ๊ฐ์ ๋ฐ๋ก ์ ๋์ ์ผ๋ก ๋ฐ๊ฒ ๋ง๋ค์ด์ฃผ์ด์ผ ํ๋ค.
import requests
from bs4 import BeatifulSoup as BS
url = "http://gs25.gsretail.com/gscvs/ko/store-services/locations#;"
r = requests.get(url)
bs = BS(r.text)
# ์ ์ํ ๋๋ง๋ค ํค ๋ฐ๊ธ์ ํด์ค๋ค.
csrf = bs.find("form", id="CSRFForm").find("input")['value']
csrf ํ ํฐ ๊ฐ์ ๊ฒฝ์ฐ, '์ธ์ '์ด ์ ์ง๋ ์ํ์๋ง ์ ํจํ๊ธฐ ๋๋ฌธ์ ๊ณ์ ์ธ์ ์ ์ ์งํ ์ ์๋๋ก ํด์ผํ๋ค.
payload = {"pageNum" : "1",
"pageSize" : "100",
"searchShopName" : "",
"searchSido" : "11",
"searchGugun" : "",
"searchDong" : "",
"searchType" : "",
"searchTypeService" : "0",
"searchTypeToto" : "0",
"searchTypeCafe25" : "0",
"searchTypeInstant" : "0",
"searchTypeDrug" : "0",
"searchTypeSelf25" : "0",
"searchTypePost" : "0",
"searchTypeATM" : "0",
"searchTypeWithdrawal" : "0",
"searchTypeTaxrefund" : "0",
"searchTypeSmartAtm" : "0",
"searchTypeSelfCookingUtensils" : "0",
"searchTypeDeliveryService" : "0",
}
post_url = "http://gs25.gsretail.com/gscvs/ko/store-services/locationList?CSRFToken={}"
with requests.Session() as s: # ์ธ์
์ฐ๊ฒฐ ์ํ๋ก ๋ง๋ค๊ธฐ
r = s.get(url)
bs = BS(r.text)
csrf = bs.find("form", id="CSRFForm").find("input")['value']
payload['pageSize'] = 5000
r2 = s.post(post_url.format(csrf), data=payload)
์ ๊ตญ GS25 ํธ์์ ๋งค์ฅ ์ง์ญ ์กฐํํ๊ธฐ
์์ ๊ฒ์ ์ข ํฉํ์ฌ ์ ์ฉํ๊ณ ์ง์ญ๋ค์ ์กฐํํ๋ฉด ์๋์ ๊ฐ๋ค.
import json
import pandas as pd
# ์ง์ญ๊ตฌ๋ถ '์/๋'์ key, value ๊ฐ ์ ์ฅ
master = {x.text : x['value'] for x in bs.find("select", id="stb1").findAll("option")[1:]}
post_url = "http://gs25.gsretail.com/gscvs/ko/store-services/locationList?CSRFToken={}"
total = []
with requests.Session() as s:
r = s.get(url)
bs = BS(r.text)
csrf = bs.find("form", id="CSRFForm").find("input")['value']
for code in master.values():
payload['pageSize'] = 5000
payload['searchSido'] = code
r2 = s.post(post_url.format(csrf), data=payload)
total.append(pd.DataFrame(json.loads(r2.json())['results']))
์ธ์ ์ ๊ณ์ ์ ์งํ๋๋ก ๋ง๋ค๊ณ , ๊ฒ์ ๊ฒฐ๊ณผ๋ค์ ์ ์ฅํ๋ค.
gs = pd.DataFrame(json.loads(r2.json())['results'])
# gs ์ total df ๋ถ์ด๊ธฐ
gs = pd.concat(total)
# ์ฃผ์ ๋ฐ์ดํฐ์์ ์/๋ ์ด๋ฆ๋ง ์๋ผ ์ ์ฅํ๊ธฐ
gs['์'] = gs['address'].apply(lambda x : x.split()[0])
gs['์'].value_counts() # ์/๋ ๋ณ๋ก GS25 ํธ์์ ๊ฐ์
์ด๋ ๊ฒ, ์/๋ ๋ณ๋ก GS25 ํธ์์ ๊ฐ์๋ ๊ตฌํ ์ ์๋ค.
์ ๊ตญ GS25 ํธ์์ ๋งค์ฅ ์๋น์ค ํ์ธํ๊ธฐ
์ํ๋ ๋ฐ์ดํฐ๋ฅผ ์ป๊ธฐ ์ํด column ๋ค์ ์ ๊ทผํ๋ฉฐ, ์๋ก์ด ์ ๋ณด๋ฅผ ํ์ธํ ์ ์๋ค.
gs['offeringService'].apply(lambda x : 'drug' in x) # ๊ฒฐ์ธก์น๊ฐ ์์ด์ error ๋ฐ์
gs.loc[gs['offeringService'].notnull(), '์๋ฃ'] = gs[gs['offeringService'].notnull()]['offeringService'].apply(lambda x : 'Y' if 'drug' in x else 'N')
gs['์๋ฃ'].value_counts(normalize=True) # ์ ๊ตญ gs ์ฝ ํ๋ ๋งค์ฅ ๋น์จ
# 11 ๊ฐ ์๋น์ค ๋คํ๋ ํธ์์ ํ์ธ
gs.loc[gs['offeringService'].notnull(), '์๋ฃ'] = gs[gs['offeringService'].notnull()]['offeringService'].apply(lambda x : len(x) == 11)
# 11 ๊ฐ ์๋น์ค๊ฐ ๋ญ์ง ๋ณด๊ธฐ
gs.loc[gs['offeringService'].notnull(), '์๋ฃ'] = gs[gs['offeringService'].notnull()]['offeringService'].apply(lambda x : len(x) == 11)['offeringServie'].values()
'Python > [๊ธฐ์ด ๊ฐ์ ์ ๋ฆฌ]' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
python ๊ธฐ์ด 10 (0) | 2023.03.03 |
---|---|
python ๊ธฐ์ด 9 (0) | 2023.03.01 |
python ๊ธฐ์ด 7 (0) | 2023.02.26 |
python ๊ธฐ์ด 6 (0) | 2023.02.26 |
python ๊ธฐ์ด 5 (0) | 2023.02.26 |