Python撰寫爬蟲程式
今天心血來潮來練習一下以前學過的python,先找一個範例來練練手
以下是爬蟲程式爬台灣銀行黃金資料:
#台灣銀行黃金價格
import time
import urllib.request as request
from bs4 import BeautifulSoup as sp
import requests
local_time = time.ctime(time.time())
url="https://rate.bot.com.tw/gold?Lang=zh-TW" ###
with request.urlopen(url) as response:
data=response.read().decode("utf-8")
root=sp(data,"html.parser")
gold_in=(root.find_all("td")[5].text.replace("回售","").strip()) ###
gold_out=(root.find_all("td")[2].text.replace("買進","").strip()) ###
s1=("\nGold"+"\n銀行買進: "+gold_in+"\n銀行賣出: "+gold_out) ###
note=local_time+s1
print(s1)
# 黃金金價買價 加入每一秒自動爬蟲一次
import time
import urllib.request as request
from bs4 import BeautifulSoup as sp
import requests
local_time = time.ctime(time.time())
j=0
while j==0:
url="https://rate.bot.com.tw/gold?Lang=zh-TW" ###
with request.urlopen(url) as response:
data=response.read().decode("utf-8")
root=sp(data,"html.parser")
gold_in=(root.find_all("td")[5].text.replace("回售","").strip()) ###
gold_out=(root.find_all("td")[2].text.replace("買進","").strip()) ###
s1=("\nGold"+"\n銀行買進: "+gold_in+"\n銀行賣出: "+gold_out) ###
note=local_time+s1
print(s1)
time.sleep(1)
每秒更新買進和回售價格
參考資料:
https://rate.bot.com.tw/gold?Lang=zh-TW
https://minkuanchen.medium.com/python%E7%88%AC%E8%9F%B2-%E7%88%AC%E5%8F%96%E9%BB%83%E9%87%91%E5%83%B9%E6%A0%BC%E4%B8%A6%E7%95%AB%E5%9C%96-83c270b5ced9
https://rate.bot.com.tw/gold/chart/day/TWD
https://www.youtube.com/watch?v=-c5rrzjsN34
留言
張貼留言