import requests
import pandas as pd
# Token 구하기
auth = requests.auth.HTTPBasicAuth(CLIENT_ID, SECRET_KEY)
data = {
'grant_type' : 'password',
'username' : username,
'password' : password
}
headers = {'User-AGent': 'MyAPI/0.0.1'}
res = requests.post('https://www.reddit.com/api/v1/access_token',
auth = auth,
data = data,
headers = headers)
TOKEN = res.json()['access_token']
headers = {**headers, **{'Authorization': f'bearer {TOKEN}'}}
# API 에서 cryptocurrency 에 대해 hot 한 주제를 100개 가져와서 reddit.csv로 저장
res = requests.get('https://oauth.reddit.com/r/cryptocurrency/hot', headers = headers,
params={'limit':'100'})
df = pd.DataFrame()
for post in res.json()['data']['children']:
df = df.append({
'title' : post['data']['title'],
'num_comments' : post['data']['num_comments'],
'url' : post['data']['url'],
'ups' : post['data']['ups'],
'downs' : post['data']['downs'],
'score' : post['data']['score'],
}, ignore_index=True)
df.sort_values(by=['num_comments'], inplace=True, ascending=False)
df.to_csv("../temp/reddit1.csv", encoding='utf-8-sig')
print(res.json())
반응형
'2. Data Science Basics > Python' 카테고리의 다른 글
Python 을 이용하여 MDD / Sharp Ratio 구하기 (0) | 2022.01.03 |
---|---|
Data Science 인터뷰 질문 및 답변 (작성중) (0) | 2022.01.03 |
A/B Testing 란? (0) | 2021.08.24 |
간단한 NLP 모델로 WSJ 부정적인 기사만 crawling 하기 (0) | 2021.08.23 |
VAR을 이용하여 Kospi 예측해보기 (0) | 2021.08.19 |
댓글