# Python 應用專題 整合(高三自然選修)
###### tags:`110科技應用專題 30118吳云禎`
## Python 應用專題01
- [x] 01.folium畫地圖
- [x] 02.Google 奇境衛星圖
- [x] 03.geocoder 找 gps 座標
- [x] 04.Google 翻譯
- [x] 05.文字轉語音~讓電腦說話
- [x] 06.語言學習機
### 01.folium畫地圖
* 程式碼
```python=
# 安裝套件
!pip install -q folium
# 載入需要的模組
import folium
# 給定城市名稱
city = "阮叨"
# 我家的 gps 座標
city_gps = [23.217254,120.096102]
# 用 folium 畫地圖,並放到變數m裡面去。
m =folium.Map(location=city_gps,zoom_start=17)
# 在地圖上的location的座標加上一個 Marker。
folium.Marker(location=city_gps, popup=city).add_to(m)
# 顯示出地圖
m
```
* 結果

* 程式碼連結
[我家的地圖](https://colab.research.google.com/drive/11IJfCJY4FGL7iwllB-GDxeQ0dFe7fCb3?usp=sharing)
### 02.找出google的奇景
* 程式碼
```python=
# 安裝套件
!pip install -q folium
attr = "default"
tiles='http://mt2.google.cn/vt/lyrs=s&hl=zh-TW&gl=cn&x={x}&y={y}&z={z}'
# 載入需要的模組
import folium
# 給定城市名稱
city = "阿塔卡馬巨人畫像"
# 我家的 gps 座標
city_gps = [-19.949156, -69.633842]
# 用 folium 畫地圖,並放到變數m裡面去。
m = folium.Map( location=[-19.949156, -69.633842],zoom_start=35, tiles=tiles,attr=attr,)
# 在地圖上的location的座標加上一個 Marker。
folium.Marker(location=city_gps, popup=city).add_to(m)
# 顯示出地圖
m
```
* 結果

* 程式碼連結
[google奇景](https://colab.research.google.com/drive/11IJfCJY4FGL7iwllB-GDxeQ0dFe7fCb3?usp=sharing)
### 03.輸入地址找出GPS座標並畫出地圖
* 程式碼
```python=
# 給定地址
print("請輸入地址:",end=" ")
city = input()
# 使用geocoder取得特定住址的GPS座標
city_gps = geocoder.osm(city).latlng
8
# 用 folium 畫地圖,並放到變數m裡面去。
m =folium.Map(location=city_gps,zoom_start=16)
# 在地圖上的location的座標加上一個 Marker。
folium.Marker(location=city_gps, popup=city).add_to(m)
# 顯示出地圖
print("經緯度座標:{0}".format(city_gps))
m
```
* 結果

* 程式碼連結
[03.輸入地址找出GPS座標並畫出地圖](https://colab.research.google.com/drive/1jdYZFx_JMC2dBdjAjDGIrZLdcXsKv-4r?usp=sharing)
### 04.引用google做多國語言翻譯器

```python=
# 外部安裝 googletrans 模組
!pip install googletrans==4.0.0-rc1 #目前最新可行版
# 載入需要的模組
from googletrans import Translator
# 查看文字翻譯可用語言
import googletrans
googletrans.LANGCODES
# 測試一下中翻英
print("請輸入要翻譯的字詞:",end=" ")
text=input()
translator = Translator()
result = translator.translate(text,dest='en').text
a=translator.translate(text,dest='zh-tw').text
b=translator.translate(text,dest='ja').text
c=translator.translate(text,dest='ko').text
d=translator.translate(text,dest='de').text
v=translator.translate(text,dest='vi').text
print("翻譯結果(英文):{0}".format(result))
print("翻譯結果(中文):{0}".format(a))
print("翻譯結果(日文):{0}".format(b))
print("翻譯結果(韓文):{0}".format(c))
print("翻譯結果(德文):{0}".format(d))
print("翻譯結果(越文):{0}".format(v))
```
[04.引用google做多國語言翻譯器](https://colab.research.google.com/drive/1jY86LEZgetkriLt7e7d4poVl4q3kf29w?usp=sharing)
### 05.文字轉語音

### 06.語言學習機
[06.語言學習機](https://colab.research.google.com/drive/10fQo-JL8zCBEzTIUUjpR8TKeHeJdq6vP?usp=sharing)

```python=
print("輸入選擇翻譯語言(1.中→英 2.英→中):",end=" ")
text=int(input())
print("請輸入要翻譯的字詞:",end=" ")
a=input()
translator = Translator()
if(text==1):
a=translator.translate(a,dest='en').text
sound_file=a+".mp3"
speak(a,'en')
Audio(sound_file,autoplay=True)
print("翻譯結果:{0}".format(a))
else:
if(text==2):
a = translator.translate(a,dest='zh-tw').text
sound_file=a+".mp3"
speak(a,'zh-tw')
Audio(sound_file,autoplay=True)
print("翻譯結果:{0}".format(a))
```
## Python 應用專題02~網路爬蟲A
- [x] 01 讀取北中網頁網站
- [x] 02 解析基本網頁
- [x] 03 解析學校BMSH網頁
- [x] 04 解析新聞網站~中時新聞網
- [x] 05 解析新聞網站~Yahoo新聞
- [ ] 06 綜合練習~ETtoday旅遊雲
### 01.讀取北中網頁資料
[讀取北中網頁資料](https://colab.research.google.com/drive/16fxOFJ7AGzLJtXhgRp4A_5wwnUq4oPv_?usp=sharing)
```python=
import requests
url= 'https://www.bmsh.tn.edu.tw'
html = requests.get(url)
html.encoding="utf-8"
# print(html.text)
htmllist=html.text.splitlines()
# print(htmllist)
n=0
print("請輸入須查詢次數之字串:",end=" ")
s=str(input())
for row in htmllist:
if s in row:
n=n+1
print("找到{}次!".format(n))
```

### 02.解析基本網頁
[02.解析基本網頁](https://colab.research.google.com/drive/1nIqAe3jxebUk8K4yrplAPDptRQq2FrTO?usp=sharing)

```python=
from requests.api import request
import requests
from bs4 import BeautifulSoup
url= 'http://www.bmsh.tn.edu.tw'
html = requests.get(url)
html.encoding="utf-8"
html_doc=requests.get(url)
# print(html_doc)
soup=BeautifulSoup(html_doc.text,'html.parser')
# 直接印內容
# print(soup)
# 有編排含內縮的內容
# print(soup.prettify())
# 網頁標題 HTML 標題
title_tag = soup.title
print(title_tag)
# 所有的超連結
a_tags = soup.find_all('a')
for tag in a_tags:
# 輸出超連結的文字
print(tag.string)
for tag in a_tags:
#輸出超連結網址
print(tag.get('href'))
```
### 03.解析學校BMSH網頁
[03.解析學校BMSH網頁](https://colab.research.google.com/drive/12IC3mhhIKH3BQpevN2VIVreg_Nbms1Nc?usp=sharing)

```python=
import requests
from bs4 import BeautifulSoup
# 下載 北中網頁首頁內容
url = 'http://www.bmsh.tn.edu.tw/'
r = requests.get(url)
# 確認是否成功
if r.status_code ==requests.codes.ok:
# 解析
soup = BeautifulSoup(r.text,'html.parser')
a_tag=soup.find_all("a")#, class_='title')
for tag in a_tag:
# 輸出
print("標題:{}\n -> 網址:{}".format(tag.string,tag.get('href')))
```
### 04.解析新聞網站~中時新聞網
[04.解析新聞網站~中時新聞網](https://colab.research.google.com/drive/1XlnClXUlg-CWYA0dfERa2IjjMD_A2Wex?usp=sharing)

```python=
from requests.api import request
import requests
from bs4 import BeautifulSoup
url= 'https://www.chinatimes.com/?chdtv'
html = requests.get(url)
html.encoding="utf-8"
html_doc=requests.get(url)
soup=BeautifulSoup(html_doc.text,'html.parser')
print(soup.prettify())
from requests.api import request
import requests
from bs4 import BeautifulSoup
url= 'https://www.chinatimes.com/?chdtv'
r = requests.get(url)
if r.status_code == requests.codes.ok:
soup = BeautifulSoup(r.text,'html.parser')
titles = soup.find_all("h4",class_="title")
#print(titles)
for title in titles:
print(title.select_one("a").getText())
print(title.select_one("a").get('href'))
```
### 05.解析新聞網站~Yahoo新聞
[05.解析新聞網站~Yahoo新聞](https://colab.research.google.com/drive/1gbV1TpRIbFR3Wmd5ND3fyiGwLkQA8Z7u?usp=sharing)

```python=
import requests
from bs4 import BeautifulSoup
r = requests.get('https://tw.yahoo.com/')
if r.status_code == requests.codes.ok:
soup = BeautifulSoup(r.text,'html.parser')
#以CSS的class抓出各類頭條新聞
stories = soup.find_all("div",class_="container")
#利用BS4擷取資料格式為bs4.element.ResultSet,可以轉成list
print(type(stories))
list1=stories[0:1]
print(list1[0].text)
for s in stories:
print("標題;"+s.text)
for i in range(1,11,1):
print(i,".",list1[0].text.split(" ")[i])
```
### 06.綜合練習~ETtoday旅遊雲