tweet.py
#-- coding: utf-8 --
#Required library
import urllib.request as request
from bs4 import BeautifulSoup
from requests_oauthlib import OAuth1Session
#Set various twitter keys(Get the key yourself)
CK = ‘Consumer Key’
CS = ‘Consumer Secret’
AT = ‘Access Token’
AS = ‘Access Token Secret’
#URL for posting tweets
url = “https://api.twitter.com/1.1/statuses/update.json”
#OAuth authentication
twitter = OAuth1Session(CK, CS, AT, AS)
#Access the youtube soaring site
response = request.urlopen(‘https://www.youtube.com/feed/trending’)
body = response.read()
#Parse HTML
soup = BeautifulSoup(body, “lxml”)
#Scraping by specifying the class attribute of the soaring site
msg = soup.find_all(class_=’yt-uix-sessionlink yt-uix-tile-link yt-ui-ellipsis yt-ui-ellipsis-2 spf-link ‘)
status =“Youtube featured video\n”
#Substitute the three video titles from the top of the soaring site into the status variable
for i in range(0,3):
status +=‘・’+ msg[i]['title'] + ‘\n’
#Tweet body
params = {“status”: status}
#Post to twitter
req = twitter.post(url, params = params)
#Output to console
print (status)
Recommended Posts