praw
๋๋ ์ค์ ํด์ผ ํ๋ ๋ฌธ์ํ๋์ง ์์ ๊ตฌ์ฑ ํ์ผ๊ณผ์ ํธํ์ฑ ๋ฌธ์ ๊ฐ ์๋ ๊ฒ ๊ฐ์ต๋๋ค. ์ด ์
์ ์คํํ ๋:
#adding a number to the end of the %run call with get the ith top post.
%run top_showerthoughts_submissions.py 2
print("Post contents: \n")
print(top_post)
์ด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
KeyError Traceback (most recent call last)
/opt/conda/lib/python3.6/configparser.py in items(self, section, raw, vars)
842 try:
--> 843 d.update(self._sections[section])
844 except KeyError:
KeyError: 'BayesianMethodsForHackers'
During handling of the above exception, another exception occurred:
NoSectionError Traceback (most recent call last)
/opt/notebooks/Chapter4_TheGreatestTheoremNeverTold/top_showerthoughts_submissions.py in <module>()
7
8
----> 9 reddit = praw.Reddit("BayesianMethodsForHackers")
10 subreddit = reddit.get_subreddit("showerthoughts")
11
/opt/conda/lib/python3.6/site-packages/praw/reddit.py in __init__(self, site_name, **config_settings)
101 self._unique_counter = 0
102 self.config = Config(site_name or os.getenv('praw_site') or 'DEFAULT',
--> 103 **config_settings)
104
105 required_message = ('Required configuration setting {!r} missing. \n'
/opt/conda/lib/python3.6/site-packages/praw/config.py in __init__(self, site_name, **settings)
64
65 self._settings = settings
---> 66 self.custom = dict(Config.CONFIG.items(site_name), **settings)
67
68 self.client_id = self.client_secret = self.oauth_url = None
/opt/conda/lib/python3.6/configparser.py in items(self, section, raw, vars)
844 except KeyError:
845 if section != self.default_section:
--> 846 raise NoSectionError(section)
847 # Update with the entry specific variables
848 if vars:
NoSectionError: No section: 'BayesianMethodsForHackers'
Post contents:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-7-be2145cbdc50> in <module>()
3
4 print("Post contents: \n")
----> 5 print(top_post)
NameError: name 'top_post' is not defined
praw==4.4.0
๋ฒ์ ์ ์คํ ์ค์
๋๋ค.
๋๋ ๊ฐ์ ๋ฌธ์ ๊ฐ ์์๋ค. praw API๊ฐ v4์์ ํฌ๊ฒ ๋ณ๊ฒฝ๋ ๊ฒ์ผ๋ก ๋ณด์ ๋๋ค. praw 3.6.1์ ์ค์นํ์ฌ ์ฝ๋๋ฅผ ์๋์ํฌ ์ ์์์ง๋ง ์ต์ ๋ฒ์ ์์ ์๋ํ๋๋ก ์ฝ๋๋ฅผ ์ ๋ฐ์ดํธํ ์ ์๋ค๋ฉด ์ข์ ๊ฒ์ ๋๋ค.
๋ค, ์ฌ๊ธฐ๋ ๋ง์ฐฌ๊ฐ์ง์ ๋๋ค. ์๋ก์ด PRAW์ ๋ ๋ง์ ์ธ์ฆ์ด ํ์ํ ๊ฒ ๊ฐ์ต๋๋ค. ๋ด๊ฐ ์ด๊ฒ์ ์ถ๊ฐํ์ ๋ ๊ด์ฐ์ ์ ์คํ๋์์ต๋๋ค.
reddit = praw.Reddit(client_id=[my id],
client_secret=[my secret],
user_agent=[a random string explaining my 'app'],
username=[my reddit username],
password=[my reddit password)
๋ ๋ง์ ์ธ์ฆ์ด ํ์ํ ๊ฒ ์ธ์๋ PRAW ๊ฐ๋ฐ์๋ ๋ฉ์๋ ์ด๋ฆ ์ค ์ผ๋ถ๋ฅผ ๋ณ๊ฒฝํ๊ณ ์ผ๋ถ ํญ๋ชฉ์ ์ฎ๊ฒผ์ต๋๋ค(์ฆ, ์ด์ ์ ์ถ ๊ฐ์ฒด์์ upvote_ratio
์ ์ก์ธ์คํ ์ ์์).
for sub in top_submissions:
try:
ratio = sub.upvote_ratio
ups = int(round((ratio*sub.score)/(2*ratio - 1)) if ratio != 0.5 else round(sub.score/2))
upvotes.append(sub.ups)
downvotes.append(ups - sub.score)
contents.append(sub.title)
except Exception as e:
raise e
PRAW 6์ ์ฌ์ฉํ์ฌ ์ด๊ฒ์ ์๋์ํค๋ ค๋ฉด ๋ช ๊ฐ์ง ์กฐ์ ์ ํด์ผ ํ์ต๋๋ค. ํํธ๋ฅผ ์ฃผ์ ์ด์ ๋๊ธ ์์ฑ์์๊ฒ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
reddit = praw.Reddit(
client_id=client_id,
client_secret=client_secret,
user_agent=user_agent,
username=username,
password=password,
)
subreddit = reddit.subreddit("showerthoughts")
# go by timespan - 'hour', 'day', 'week', 'month', 'year', 'all'
# might need to go longer than an hour to get entries...
top_submissions = subreddit.top('hour')
n_sub = int(sys.argv[1]) if len(sys.argv) > 1 else 1
i = 0
while i < n_sub:
top_submission = next(top_submissions)
i += 1
top_post = top_submission.title
upvotes = []
downvotes = []
contents = []
for sub in top_submissions:
try:
ratio = sub.upvote_ratio
ups = int(round((ratio*sub.score)/(2*ratio - 1))
if ratio != 0.5 else round(sub.score/2))
upvotes.append(ups)
downvotes.append(ups - sub.score)
contents.append(sub.title)
except Exception as e:
continue
limit
arg ๋ top()
์์ ์๋ํ๋ค๊ณ ์ธ๊ธํ์ด์ผ ํ์ต๋๋ค. ์๋ฅผ ๋ค๋ฉด
top_submissions = subreddit.top('hour', limit=5)
top_submissions = subreddit.top('all', limit=20)
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋ค, ์ฌ๊ธฐ๋ ๋ง์ฐฌ๊ฐ์ง์ ๋๋ค. ์๋ก์ด PRAW์ ๋ ๋ง์ ์ธ์ฆ์ด ํ์ํ ๊ฒ ๊ฐ์ต๋๋ค. ๋ด๊ฐ ์ด๊ฒ์ ์ถ๊ฐํ์ ๋ ๊ด์ฐ์ ์ ์คํ๋์์ต๋๋ค.
๋ ๋ง์ ์ธ์ฆ์ด ํ์ํ ๊ฒ ์ธ์๋ PRAW ๊ฐ๋ฐ์๋ ๋ฉ์๋ ์ด๋ฆ ์ค ์ผ๋ถ๋ฅผ ๋ณ๊ฒฝํ๊ณ ์ผ๋ถ ํญ๋ชฉ์ ์ฎ๊ฒผ์ต๋๋ค(์ฆ, ์ด์ ์ ์ถ ๊ฐ์ฒด์์
upvote_ratio
์ ์ก์ธ์คํ ์ ์์).