Movie Line Bot

講者:邱妍瑛

日期:2020 / 02 / 12

大綱

  • 前置準備
  • 製作電影查詢 Line Bot
  • 發佈到 Heroku 上

Outline

前置準備

Preparation

新建資料夾並開啟虛擬環境

$ mkdir 資料夾名稱
$ cd 到剛剛新增的資料夾
$ pipenv --three
$ pipenv shell

一共有六個套件要安裝

$ pipenv install flask
$ pipenv install line_bot_sdk
$ pipenv install gunicorn
$ pipenv install requests
$ pipenv install BeautifulSoup4
$ pipenv install lxml

裝完可以打開 pipfile 確認都有安裝成功

製作電影查詢 Line Bot

coding time

步驟

  1. 取得電影網資料
  2. 抓取電影名稱
  3. 整理資料
  4. 加上數量限制
  5. 寫進 Line Bot

Step

透過爬蟲取得電影網資料

Step 1

目標是爬取開眼電影網的本週新片:

http://www.atmovies.com.tw/movie/new/

import requests
from bs4 import BeautifulSoup
import requests
from bs4 import BeautifulSoup

#GET請求
r = requests.get('http://www.atmovies.com.tw/movie/new/')
r.encoding = 'utf-8'
import requests
from bs4 import BeautifulSoup

#GET請求
r = requests.get('http://www.atmovies.com.tw/movie/new/')
r.encoding = 'utf-8'

soup = BeautifulSoup(r.text, 'lxml')
print(soup)

Python 在解析網頁時預設用 Unicode 去解析,輸出格式會與系統編碼格式不同,因而導致中文輸出亂碼。

r.encoding = 'utf-8'

抓取電影名稱

Step 2

 <a href="連結">電影名稱 </a>

打開網頁的開發人員工具

import requests
from bs4 import BeautifulSoup

r = requests.get('http://www.atmovies.com.tw/movie/new/')
r.encoding = 'utf-8'

soup = BeautifulSoup(r.text, 'lxml')

filmTitle = soup.select('div.filmTitle a')
print(filmTitle)
[<a href="/movie/fben51502397/">絕地戰警 FOR LIFE Bad Boys for Life </a>,
<a href="/movie/fjen64916630/">不完美的正義 Just Mercy </a>,
<a href="/movie/fskr58999329/">青春催落去 Start-Up </a>
......

此時的輸出結果

整理取得的資料

Step 3

[<a href="/movie/fben51502397/">絕地戰警 FOR LIFE Bad Boys for Life </a>,
<a href="/movie/fjen64916630/">不完美的正義 Just Mercy </a>,
<a href="/movie/fskr58999329/">青春催落去 Start-Up </a>
......

原本的輸出結果

import requests
from bs4 import BeautifulSoup

r = requests.get('http://www.atmovies.com.tw/movie/new/')
r.encoding = 'utf-8'

soup = BeautifulSoup(r.text, 'lxml')

filmTitle = soup.select('div.filmTitle a')

print(filmTitle[0].text)
print("http://www.atmovies.com.tw" + filmTitle[0]['href'])

補充 - href

在 html 的語法中,

超連結的語法是這樣表示的:

參考資料:HTML a href 連結屬性

www.wibibi.com/info.php?tid=240

<a href="要連結的 URL 會放在這裡">

加上數量限制

Step 4

由於是要讓使用者在 line 裡面使用,

因此一次給太多數量的電影或多或少會造成使用者的困擾,

所以加上數量限制是必要的。

計算傳了幾個?

限制數量?

計算傳了幾個

i = 0
num = ['one', 'two', 'three']
for i in range(0,3):
    print(num[i],i)

平常我們在用 for 迴圈時大概會是這樣:

0 one
1 two
2 three

此時的輸出結果:

num = ['one', 'two', 'three']
for i, element in enumerate(num):
     print(i, element)

可以使用 enumerate() 函数更簡潔的達成此目標

import requests
from bs4 import BeautifulSoup

r = requests.get('http://www.atmovies.com.tw/movie/new/')
r.encoding = 'utf-8'

soup = BeautifulSoup(r.text, 'lxml')

filmTitle = soup.select('div.filmTitle a')

content = ""

for i, data in enumerate(filmTitle):
    print(i) #可以印出 i 來看看
    content += data.text + "\n" + "http://www.atmovies.com.tw/" 
    		+ data['href'] + "\n\n"

print(content)

寫進 line bot 內

Step 5

這邊是設定當使用者輸入「本周新片」時,就會觸發 class 裡面寫的程式碼。

在本地端測試無誤後,即可發佈到 heroku 上,就完成電影查詢 line bot 了。

if event.message.text == '本周新片':

發佈到 Heroku 上

coding time

web: gunicorn 你的檔名(不用加副檔名):app
# 像是 web: gunicorn app:app

新增  Procfile

.env

新增 .gitignore

.gitignore 的檔案中寫入不想被上傳的檔案像是這邊的 .env

$ quit
$ pipenv shell

重新開啟虛擬環境

先到 Heroku 的頁面點 Create New App

$ heroku login
$ cd 到放剛剛檔案的資料夾
$ git init
$ heroku git:remote -a 你剛剛新增的app的名稱

發佈到 heroku 上 -1

$ git add .
$ git commit -am "你想要留下的說明"
$ git push heroku master

發佈到 heroku 上 -2

點選 open app

  

複製網址並更改 line 那邊的網址

Movie Line Bot

By oneone

Movie Line Bot

SIRLA - Winter Workshop

  • 119