Skip to content

Instantly share code, notes, and snippets.

@aweekj
Created October 28, 2016 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aweekj/9578656cf4bef7820dbd90ac511920eb to your computer and use it in GitHub Desktop.
Save aweekj/9578656cf4bef7820dbd90ac511920eb to your computer and use it in GitHub Desktop.
Twitter Bot Tutorial (2)
(env) $ pip install pytz beautifulsoup4
(env) $ touch weather.py
(env) $ python bot.py
# -*- coding: utf-8 -*-
import tweepy
from secret import *
from weather import Weather # 추가
class TwitterAPI:
def __init__(self):
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
self.api = tweepy.API(auth)
def tweet(self, message):
self.api.update_status(status=message)
if __name__ == "__main__":
twitter = TwitterAPI()
weather = Weather() # 추가
twitter.tweet(weather.get_text()) # 수정
#-*- coding: utf-8 -*-
import urllib.request
from datetime import datetime
import pytz
from bs4 import BeautifulSoup
class Weather():
@staticmethod
def get_text():
url = "http://weather.naver.com/rgn/townWetr.nhn?naverRgnCd=09230109"
page = urllib.request.urlopen(url)
weather = BeautifulSoup(page, "html.parser")
today = datetime.now(pytz.timezone('Asia/Seoul')).strftime("%Y년 %m월 %d일")
text = "{} 오늘 날씨\n".format(today)
for cell in weather.find_all("div", "cell", limit=2):
when = cell.find("b").string # 오전, 오후
temperature = cell.find("span", "temp").string
weather = cell.img['alt']
rain = cell.find("strong").string
text += "{}: {}℃, {}, 강수 확률 {}%\n".format(when, temperature, weather, rain)
return text
if __name__ == "__main__":
w = Weather()
print(w.get_text())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment