Last active
January 7, 2018 11:26
-
-
Save miya/6ac6b34dc17b7b5085ee0279d09ee2f6 to your computer and use it in GitHub Desktop.
bitFlyerをスクレイピングしてビットコインの価格をツイートするやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import urllib | |
import tweepy | |
import datetime | |
from bs4 import BeautifulSoup as b4 | |
todaydetail = datetime.datetime.today() | |
today = todaydetail.strftime("%m/%d %H:%M") | |
#https://apps.twitter.com にて consumerkey等を取得して各自で代入して下さい | |
def Tweet(text): | |
consumer_key = 'consumer_key' | |
consumer_secret = 'consumer_secret' | |
access_key = 'access_key' | |
access_secret = 'access_secret' | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_key, access_secret) | |
api = tweepy.API(auth) | |
api.update_status(text) | |
def BTC(): | |
url = 'https://bitflyer.jp/' #bitFlyerの公式サイトからスクレピングします。 | |
headers = {"User-Agent":"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"} #UserAgentを変更します。 | |
req = urllib.request.Request(url,None,headers) | |
res = urllib.request.urlopen(req) | |
soup = b4(res,"html.parser") | |
soup = str(soup) | |
pa = 'none;">(.+)</div>' #正規表現で文字列を抽出します。 | |
ma = re.findall(pa,soup) #上の正規表現にマッチした文字列(買値と売値)をすべて取り出して、変数maにリストとして格納します。 | |
x = today + ' 現在のビットコインの価格\nBTC/JPY ' + 'ask: ' + ma[0] + ' / ' + 'bid: ' + ma[1] #ツイート内容を編集します。 | |
return x | |
if __name__ == '__main__': | |
Tweet(BTC()) #BTC()の戻り値を代入してツイートします。crontabで時間指定してbot化しました。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment