今日練習爬蟲,將爬下來的資料傳到資料庫(今日時間不夠,下次上課再進行)
BTC價格
import requests
from bs4 import BeautifulSoup
import json
import datetime
#target_url = 'https://coinmarketcap.com/currencies/bitcoin/historical-data/'
start_time = '1628006400'
end_time = '1628236123'
target_api = 'https://api.coinmarketcap.com/data-api/v3/cryptocurrency/historical?id=1&convertId=2781&timeStart='+ start_time +'&timeEnd='+end_time
#headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}
data = requests.get( target_api , headers = headers)
json_data = json.loads(data.text)
time = json_data['data']['quotes'][0]['timeOpen']
price = json_data['data']['quotes'][0]['quote']['open']
data_btc = json_data['data']['quotes']
for price in data_btc:
print (price['timeOpen'])
print ( price['quote']['open'])
2021-08-04T00:00:00.000Z
38213.33196989
2021-08-05T00:00:00.000Z
39744.51415501