5 72 5.1 72 1.5 (?) (?) Python ( ) ( ) Python : Python import 5.2 Python Python Anaconda https://www.anaconda.com/download/ Anaconda 2 3 (Python2 ) macos Linux,Windows Subsystem for Linux Linux 46
5.3 Python sudo apt-get install python3 python3-pip pip Python macos Homebrew macos Homebrew /usr/bin/ruby -e "$(curl -fssl https://raw.githubusercontent.com/homebrew/install/master/install)" Python brew install python3 5.3 Python Python Python3 Python ( ) python( python3) Helloworld.py print( Hello,world! ) 47
5.4 C:\Users\taizo>python Helloworld.py Hello,world! 5.4 os Python FindFile.py 1 #! python3 2 # -*- coding: utf-8 -*- 3 4 import os 5 import sys 6 7 folder = sys.argv[1] 8 extension = sys.argv[2] 9 #folder extension 10 for foldername, subfolders, filenames in os.walk(folder): 11 for filename in filenames: 12 if filename.lower().endswith(extension):# extension 13 print(os.path.join(foldername, filename))# 2 Python sys sys import sys.argv 1 import sys 2 for i in sys.argv: 3 print(i) argv.py C:\Users\taizo>python argv.py a b c argv.py a b c sys.argv os.walk for 3 os.path.join OS 12~13 os.walk extension( extension) lower 48
C jpg python (FindFile.py ) C:// jpg : C:\Users C:\Users npca.png npca.png c:\users\npca.png Web & Web (Crawling) (Scraping) Google https://tenki.jp 1 #! python3 2 # -*- coding: utf-8 -*- 3 4 import requests 5 import bs4 6 import smtplib 7 from email.mime.text import MIMEText 8 from email.header import Header 9 10 percent = 70 # percent 11 12 url = requests.get( https://tenki.jp/forecast/6/31/6310/28100/ ) # url URL 13 parse = bs4.beautifulsoup(url.text, "lxml") # parse url lxml HTML 14 probability = parse.select(.rain-probability )[0] 15 if int(probability.select( td )[1].text[0]) >= percent or int(probability.select( td )[2].text[0]) >= percent: 16 letter = 17 else: 18 letter = 19 20 charset = iso-2022-jp 21 message = MIMEText(letter, plain, charset) 22 message[ Subject ] = Header(.encode(charset), charset) 23 smtp_obj = smtplib.smtp( smtp.gmail.com, 587) 24 smtp_obj.ehlo() 25 smtp_obj.starttls() 26 smtp_obj.login( FROM, PASSWORD ) 27 smtp_obj.sendmail( FROM, TO, message.as_string()) 49
28 smtp_obj.quit() 12~15 HTML 20~28 requests web requests.get URL bs4 bs4 BeautifulSoup4 BeautifulSoup4 HTML bs4.beautifulsoup HTML ( ) lxml HTML (pip install lxml ) smtplib SMTP(Simple Mail Transfer Protocol) smtplib.smtp smpt Gmail smtp TLS 587 smtplib.ehlo smtp_obj.starttls smtp_obj.quit TLS smtp_obj.login Google https://support.google.com/accounts/answer/185833 smtp_obj.sendmail FROM TO MIMEText Header as_string MIMEText Header 12~15 HTML tenki.jp 50
: tenki.jp ( Ctrl+U ) rain-probability select (. ) : tenki.jp 51
rain-probability 2 1 4 td 2 3 td % 2 (6~12 12~18 ) percent 7 Windows.py.py @(python.exe ) ( ) %* OK API API(Application Programming Interface) (Wikipedia ) API Twitter YouTube Twitter API Twitter Twitter SNS API URL https://developer.twitter.com/ Twitter API Twitter API Twitter (https://apps.twitter.com/) Create New App Callback URL Keys and Access Tokens Create my access token 1 #!python3 2 # -*- coding: utf-8 -*- 3 from requests_oauthlib import OAuth1Session 4 import os 5 import time 6 import shelve 7 import requests 8 9 file = shelve.open( lasttweet ) 10 lasttweet = file[ lasttweet ] # 11 12 CONSUMER_KEY = os.environ[ KEY ] 13 CONSUMER_SECRET = os.environ[ SECRET ] 14 ACCESS_TOKEN = os.environ[ ACCESS_TOKEN ] 15 ACCESS_TOKEN_SECRET = os.environ[ ACCESS_TOKEN_SECRET ] 16 17 twitter = OAuth1Session(CONSUMER_KEY, 18 client_secret=consumer_secret, 19 resource_owner_key=access_token, 20 resource_owner_secret=access_token_secret) 52
21 22 response = twitter.get( https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=@@@@@&since_id= + lasttweet + &count=300& exclude_replies=true&include_rts=false ) 23 24 for tweet in response.json(): 25 try: 26 for image in tweet[ extended_entities ][ media ]: 27 image_url = image[ media_url ] 28 print(image_url) 29 image = requests.get(image_url) 30 image.raise_for_status() 31 image_file = open(os.path.join(, os.path.basename(image_url)), wb ) 32 for chunk in image.iter_content(100000): 33 image_file.write(chunk) 34 image_file.close() 35 time.sleep(5) 36 except: 37 pass 38 39 file.close() Twitter 22 24~37 from requests_oauthlib import OAuth1Session Twitter API OAuth OAuth shelve shelve shelve Python ID lasttweet open close lasttweet import shelve file = shelve.open( lasttweet ) file[ lasttweet ] = file.close() Twitter os.environ 22 22 json 5.1 screen_name since_id count exclude_replies include_rts (@ ) ID true false 24~37 try~except try except try 53
json URL extended_entities media media_url 29~34 raise_for_status 31=33 100000 5 ( ) 54
YouTube API YouTube Google API URL https://developers.google.com/youtube/v3/?hl=ja/ YouTube API YouTubeAPI Google API Console(https://console.developers.google.com) API YouTube Data API v3 API YouTube https://www.youtube.com/account_privacy 1 from apiclient.discovery import build 2 import webbrowser 3 import datetime 4 import shelve 5 import os 6 7 file = shelve.open( begintime ) 8 begindate = datetime.datetime(int(file[ year ]), int(file[ month ]), int(file[ day ]), nt(file[ hour ]), int(file[ minute ]), int(file[ second ])) 9 nowdate = datetime.datetime.now() 10 11 youtube = build( youtube, v3,developerkey=os.environ[ GOOGLE_KEY ]) 12 search = youtube.subscriptions().list(part= snippet, channelid= ID ).execute() 13 14 def getchannel(info): 15 for channel in info[ items ]: 16 getmovie(channel[ snippet ][ resourceid ][ channelid ]) 17 if( nextpagetoken in info.keys()): 18 nextpage = youtube.subscriptions().list(part= snippet, channelid= ID, pagetoken = info[ nextpagetoken ]).execute() 19 channelname(nextpage) 20 21 def getmovie(info): 22 channel = youtube.channels().list(part= contentdetails, id=info).execute() 23 uploads = youtube.playlistitems().list(part= snippet, playlistid=channel[ items ][0][ contentdetails ][ relatedplaylists ][ uploads ], maxresults=50).execute() 24 for movie in uploads[ items ]: 25 moviedate = datetime.datetime.strptime(movie[ snippet ][ publishedat ], %Y-%m-%dT%H:%M:%S.000Z ) 26 moviedate += datetime.timedelta(hours=9) 27 if moviedate > begindate: 28 webbrowser.open( https://www.youtube.com/watch?v= + movie[ snippet ][ resourceid ][ videoid ]) 29 30 getchannel(search) 31 32 file[ year ] = nowdate.year 33 file[ month ] = nowdate.month 34 file[ day ] = nowdate.day 35 file[ hour ] = nowdate.hour 36 file[ minute ] = nowdate.minute 37 file[ second ] = nowdate.second 38 39 file.close() from apiclient.discovery import build Python Google API Client for Python Google API pip install google-api-python-client webbrowser URL 55
datetime datetime datetime year month day hour minute second microsecond shelve shelve YouTube API YouTube API 10 build API API subscriptions().list part channelid ID (YouTube URL https://www.youtube.com/channel/ ID channel ) execute() json getchannel URL subscriptions().list items 1 5 6 nextpagetoken subscriptions().list pagetoken 2 ID getmovie getmovie channels.list() part id ID items contentdetails relatedplaylists uploads ID playlistitems().list part playlistid ID maxresults 1 items snippet publishedat 56
5.6 (2018-04-28T10:51:56.000Z) datetime datetime strptime datetime 9 9 shelve (?)! : ( ) 5.6 Web 57
5.7 5.7 Al Sweigart (2017) Python (2016) Python Twitter API https://developer.twitter.com/ YouTube API https://developers.google.com/youtube/v3/?hl=ja/ 58