iT邦幫忙

2021 iThome 鐵人賽

DAY 27
1
影片教學

文組生的Python爬蟲之旅系列 第 27

Day 27 任天堂eShop遊戲價格爬取

  • 分享至 

  • xImage
  •  

今天的影片內容為爬取任天堂線上商城遊戲的售價後,再轉換成新台幣進行價格比對
是一個我個人覺得蠻有趣的主題呦~/images/emoticon/emoticon37.gif

P.S.錄這部影片的時候有點感冒,聲音沙啞沙啞的,還請大家見諒

Yes

以下為影片中有使用到的程式碼

#香港任天堂網站
import requests, bs4

#爬取官網售價
url_1 = "https://store.nintendo.com.hk/70010000009373"
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'}
htmlfile_1 = requests.get(url_1, headers = headers)
objsoup_1 = bs4.BeautifulSoup(htmlfile_1.text, 'lxml')

price_1 = objsoup_1.find('span', class_ = 'price') #HKD 429.00
price_2 = price_1.text[4:] #429.00
price_3 = float(price_2) #轉成浮點數
print(price_3) 

#匯率
url_2 = "https://www.google.com/search?q=%E6%B8%AF%E5%B9%A3%E5%8F%B0%E5%B9%A3&oq=%E6%B8%AF%E5%B9%A3&aqs=chrome.0.69i59j69i57j0i433i512j0i512l7.7520j1j7&sourceid=chrome&ie=UTF-8"
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'}
htmlfile_2 = requests.get(url_2, headers = headers)
objsoup_2 = bs4.BeautifulSoup(htmlfile_2.text, 'lxml')

exchange_rate_1 = objsoup_2.find('span', class_ = 'DFlfde SwHCTb')
exchange_rate_2 = float(exchange_rate_1.text) 
print(exchange_rate_2)

#價格轉換
final_price = price_3 * exchange_rate_2 #當地幣值×匯率=商品價格(台幣)
print(final_price)
#日本任天堂網站
import requests, bs4

#爬取官網售價
url_1 = "https://store-jp.nintendo.com/list/software/70010000000186.html"
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'}
htmlfile_1 = requests.get(url_1, headers = headers)
objsoup_1 = bs4.BeautifulSoup(htmlfile_1.text, 'lxml')

price_1 = objsoup_1.find('div', class_ = 'productDetail--detail__price js-productMainRenderedPrice') #6,578円税込
price_2 = price_1.find('span') #6,578
price_3 = float(price_2.text[0] + price_2.text[2:]) #6578
print(price_3)

#匯率
url_2 = "https://www.google.com/search?q=%E6%97%A5%E5%B9%A3%E5%8F%B0%E5%B9%A3&sxsrf=AOaemvJz_v7oI4b11fnbGdgMitXTF06Vig%3A1633244289771&ei=gVRZYaO5LqO0qtsP6P6U-Ac&ved=0ahUKEwjj_eON1a3zAhUjmmoFHWg_BX8Q4dUDCA4&uact=5&oq=%E6%97%A5%E5%B9%A3%E5%8F%B0%E5%B9%A3&gs_lcp=Cgdnd3Mtd2l6EAMyBQgAEIAEMgUIABCABDIFCAAQgAQyBQgAEIAEMgUIABCABDIFCAAQgAQyBggAEAcQHjIGCAAQBxAeMgYIABAHEB4yBQgAEIAEOgcIIxCwAxAnOgcIABBHELADOgcIABCxAxBDOggIABCABBCxAzoECAAQQzoLCAAQgAQQsQMQgwE6BQgAEM0CSgQIQRgAUMCJKliMlypg8p8qaAJwAngAgAHtAYgBigmSAQUwLjQuM5gBAKABAcgBA8ABAQ&sclient=gws-wiz"
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'}
htmlfile_2 = requests.get(url_2, headers = headers)
objsoup_2 = bs4.BeautifulSoup(htmlfile_2.text, 'lxml')

exchange_rate_1 = objsoup_2.find('span', class_ = 'DFlfde SwHCTb')
exchange_rate_2 = float(exchange_rate_1.text)
print(exchange_rate_2)

#價格轉換
final_price = price_3 * exchange_rate_2
print(final_price)
#美國任天堂網站
import requests, bs4

#爬取官網售價
url_1 = "https://www.nintendo.com/games/detail/mario-kart-8-deluxe-switch/"
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'}
htmlfile_1 = requests.get(url_1, headers = headers)
objsoup_1 = bs4.BeautifulSoup(htmlfile_1.text, 'lxml')

price_1 = objsoup_1.find('span', class_ = 'h2 msrp') #$59.99(但不知道為甚麼有空格出現...)
price_2 = price_1.text.strip() #使用strip()將空格刪除
price_3 = float(price_2[1:]) #59.99
print(price_3)

#匯率
url_2 = "https://www.google.com/search?q=%E7%BE%8E%E9%87%91%E5%8F%B0%E5%B9%A3&sxsrf=AOaemvIXdG5ub-ANXTknvwTmkU8GO6PovA%3A1633245176880&ei=-FdZYeGONd2Lr7wProWosA0&ved=0ahUKEwjh6uS02K3zAhXdxYsBHa4CCtYQ4dUDCA4&uact=5&oq=%E7%BE%8E%E9%87%91%E5%8F%B0%E5%B9%A3&gs_lcp=Cgdnd3Mtd2l6EAMyBAgAEEMyCAgAEIAEELEDMgUIABCABDIFCAAQgAQyBQgAEIAEMgUIABCABDIFCAAQgAQyBQgAEIAEMgUIABCABDIFCAAQgAQ6CAgAEIAEELADOgcIABCwAxAeOgcIABCxAxBDOgsIABCABBCxAxCDAToFCAAQzQI6BggAEAcQHkoECEEYAVC6kg5YnfQOYIf7DmgBcAB4AIABwQOIAZ0MkgEJMS42LjEuMC4xmAEAoAEByAEKwAEB&sclient=gws-wiz"
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'}
htmlfile_2 = requests.get(url_2, headers = headers)
objsoup_2 = bs4.BeautifulSoup(htmlfile_2.text, 'lxml')

exchange_rate_1 = objsoup_2.find('span', class_ = 'DFlfde SwHCTb')
exchange_rate_2 = float(exchange_rate_1.text)
print(exchange_rate_2)

#價格轉換
final_price = price_3 * exchange_rate_2
print(final_price)
#整合程式
import requests, bs4

#place = [地區, 當地官網, 匯率查詢]
HK = ["香港", "https://store.nintendo.com.hk/70010000009373", "https://www.google.com/search?q=%E6%B8%AF%E5%B9%A3%E5%8F%B0%E5%B9%A3&oq=%E6%B8%AF%E5%B9%A3&aqs=chrome.0.69i59j69i57j0i433i512j0i512l7.7520j1j7&sourceid=chrome&ie=UTF-8"]
JP = ["日本", "https://store-jp.nintendo.com/list/software/70010000000186.html", "https://www.google.com/search?q=%E6%97%A5%E5%B9%A3%E5%8F%B0%E5%B9%A3&sxsrf=AOaemvJz_v7oI4b11fnbGdgMitXTF06Vig%3A1633244289771&ei=gVRZYaO5LqO0qtsP6P6U-Ac&ved=0ahUKEwjj_eON1a3zAhUjmmoFHWg_BX8Q4dUDCA4&uact=5&oq=%E6%97%A5%E5%B9%A3%E5%8F%B0%E5%B9%A3&gs_lcp=Cgdnd3Mtd2l6EAMyBQgAEIAEMgUIABCABDIFCAAQgAQyBQgAEIAEMgUIABCABDIFCAAQgAQyBggAEAcQHjIGCAAQBxAeMgYIABAHEB4yBQgAEIAEOgcIIxCwAxAnOgcIABBHELADOgcIABCxAxBDOggIABCABBCxAzoECAAQQzoLCAAQgAQQsQMQgwE6BQgAEM0CSgQIQRgAUMCJKliMlypg8p8qaAJwAngAgAHtAYgBigmSAQUwLjQuM5gBAKABAcgBA8ABAQ&sclient=gws-wiz"]
USA = ["美國", "https://www.nintendo.com/games/detail/mario-kart-8-deluxe-switch/", "https://www.google.com/search?q=%E7%BE%8E%E9%87%91%E5%8F%B0%E5%B9%A3&sxsrf=AOaemvIXdG5ub-ANXTknvwTmkU8GO6PovA%3A1633245176880&ei=-FdZYeGONd2Lr7wProWosA0&ved=0ahUKEwjh6uS02K3zAhXdxYsBHa4CCtYQ4dUDCA4&uact=5&oq=%E7%BE%8E%E9%87%91%E5%8F%B0%E5%B9%A3&gs_lcp=Cgdnd3Mtd2l6EAMyBAgAEEMyCAgAEIAEELEDMgUIABCABDIFCAAQgAQyBQgAEIAEMgUIABCABDIFCAAQgAQyBQgAEIAEMgUIABCABDIFCAAQgAQ6CAgAEIAEELADOgcIABCwAxAeOgcIABCxAxBDOgsIABCABBCxAxCDAToFCAAQzQI6BggAEAcQHkoECEEYAVC6kg5YnfQOYIf7DmgBcAB4AIABwQOIAZ0MkgEJMS42LjEuMC4xmAEAoAEByAEKwAEB&sclient=gws-wiz"]



def objsoup(url):
    #下載網頁並建立beautifulsoup物件
    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36'}
    htmlfile = requests.get(url, headers = headers)
    return bs4.BeautifulSoup(htmlfile.text, 'lxml')
 

 
def Rate(place):
    #查詢該地區與台幣間之匯率
    a = objsoup(place[2])
    exchange_rate_1 = a.find('span', class_ = 'DFlfde SwHCTb')
    exchange_rate_2 = float(exchange_rate_1.text)
    return exchange_rate_2


    
def find_price(place):
    #從該地區官網爬取售價(當地幣值)
    if place[0] == "日本":
        a = objsoup(place[1])
        price_1 = a.find('div', class_ = 'productDetail--detail__price js-productMainRenderedPrice') 
        price_2 = price_1.find('span') 
        return float(price_2.text[0] + price_2.text[2:])
        
    elif place[0] == "香港":
        b = objsoup(place[1])       
        price_1 = b.find('span', class_ = 'price')
        price_2 = price_1.text[4:]
        return float(price_2)
        
    else:
        c = objsoup(place[1])
        price_1 = c.find('span', class_ = 'h2 msrp')
        price_2 = price_1.text.strip()
        return float(price_2[1:])
        


print("香港的售價為:", find_price(HK)*Rate(HK))
print("日本的售價為:", find_price(JP)*Rate(JP))
print("美國的售價為:", find_price(USA)*Rate(USA))

本篇影片及程式碼僅提供研究使用,請勿大量惡意地爬取資料造成對方網頁的負擔呦!
如果在影片中有說得不太清楚或錯誤的地方,歡迎留言告訴我,謝謝您的指教。


上一篇
Day 26 Dcard熱門文章爬取
下一篇
Day 28 爛番茄影評網爬取
系列文
文組生的Python爬蟲之旅30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言