It's a hassle to check the TV section every time what the movie of this Friday Road Show is. So, I wondered if I could get the movie name of the Friday Road Show from the terminal of the PC with one command using Python web scraping.
--Display the title of the movie that will be broadcast next Friday with a single command on the terminal. --At that time, scrape the Friday Road Show lineup page (https://kinro.jointv.jp/lineup) using Python's Beautiful Soup. .. Let's look at the structure of the page to be scraped in advance.
Friday Road Show website
...
<li>
<div class="photo">
<a href='/lineup/20170414'>
<img src="https://dtg3yjoeemd2c.cloudfront.net/pic/lineup/20170414/photo01_p62bphcy8m.jpg " alt="Detective Conan: A Pure Black Nightmare" />
</a>
</div>
...
</li>
<li>
<div class="photo">
<a href='/lineup/20170421'>
<img src="https://dtg3yjoeemd2c.cloudfront.net/pic/lineup/20170421/photo01_uyxdjywd.jpg " alt="Cinderella" />
</a>
</div>
...
</li>
<li>
<div class="photo">
<a href='/lineup/20170428'>
<img src="https://dtg3yjoeemd2c.cloudfront.net/pic/lineup/20170428/photo01_9txwertpu3.jpg " alt="Wild Speed Sky Mission" />
</a>
</div>
...
</li>
...
kinro.py
#coding:utf-8
import urllib.request
import datetime
from bs4 import BeautifulSoup
def func():
html = urllib.request.urlopen("https://kinro.jointv.jp/lineup")
soup = BeautifulSoup(html, "lxml")
today = datetime.date.today()
nextFriday = today + datetime.timedelta(days = (4 - today.weekday()) % 7)
strnextFriday = nextFriday.strftime("%Y%m%d")
a = soup.find_all("a", href = "/lineup/" + strnextFriday)
tmp = a[0].find("img")
title = tmp.attrs['alt']
print(title)
if __name__ == '__main__':
func()
Open a terminal and in the same directory as this code,
$python kinro.py
Execute the command
Detective Conan: A Pure Black Nightmare#Within April 14, 2017
Cinderella#April 15, 2017~21st
If the title of the movie is displayed like, it is successful.
Of course, in .barhrc
alias kinro='python ~/my_dir/kinro.py' #Directory name matches environment
If you define this command like this, you can get the movie name of next Friday Road Show with one command of $ kinro
on any directory.
The first two lines.
kinro.py(part)
html = urllib.request.urlopen("https://kinro.jointv.jp/lineup")
soup = BeautifulSoup(html, "lxml")
Lines 3-5. I'm getting today's date and calculating the difference in days from there to next Friday.
kinro.py(part)
today = datetime.date.today()
nextFriday = today + datetime.timedelta(days = (4 - today.weekday()) % 7)
strnextFriday = nextFriday.strftime("%Y%m%d")
Lines 6-9.
kinro.py(part)
a = soup.find_all("a", href = "/lineup/" + strnextFriday)
tmp = a[0].find("img")
title = tmp.attrs['alt']
print(title)
On the 6th line
Friday Road Show website
<a href='/lineup/20170414'>
<img src="https://dtg3yjoeemd2c.cloudfront.net/pic/lineup/20170414/photo01_p62bphcy8m.jpg " alt="Detective Conan: A Pure Black Nightmare" />
</a>
Take out the part of, and further from there on the 7th line
Friday Road Show website
<img src="https://dtg3yjoeemd2c.cloudfront.net/pic/lineup/20170414/photo01_p62bphcy8m.jpg " alt="Detective Conan: A Pure Black Nightmare" />
From there on the 8th line
Detective Conan: A Pure Black Nightmare
Only the part of is taken out.
Recommended Posts