简体   繁体   中英

How can I fix the no value error in my code?

I am trying to read the data on a website using pandas and bokeh using jupyter but it returns a ValueError: no tables found 404 despite the link being valid. Any hint would be greatly appreciated. Below are my code lines:

from math import pi
import pandas
from bokeh.plotting import figure, show, output_file

df = pandas.read_html("https://coinmarketcap.com/currencies/bitcoin/historical-data/?start=20190220&end=20190320")[2][::-1]

df["Date"] = pandas.to_datetime(df["Date"])

df.rename(index = str, columns = {"Open*": "Open"}, inplace = True)
df.rename(index = str, columns = {"Close**": "Close"}, inplace = True)

inc = df.Close > df.Open
dec = df.Open > df.Close

w = 12 * 60 * 60 * 1000

tools = "pan,wheel_zoom,box_zoom,reset,save"

p = figure(x_axis_type = "datetime", tools = tools, plot_width = 1200, title = "Bitcoin Candlesticks")

p.xaxis.major_label_orientation = pi / 4
p.grid.grid_line_alpha = 0.3

p.segment(df.Date, df.High, df.Date, df.Low, color = "black")
p.vbar(df.Date[inc], w, df.Open[inc], df.Close[inc], fill_color = "#D5E1DD", line_color = "black")
p.vbar(df.Date[dec], w, df.Open[dec], df.Close[dec], fill_color = "#F2583E", line_color = "black")

output_file("bitcoin.html", title = "Bitcoin Candlesticks")

show(p)

Use requests with header to get the data

url = 'https://finance.yahoo.com/quote/BTC-USD/history?period1=1577836800&period2=1609372800&interval=1d&filter=history&frequency=1d&includeAdjustedClose=true'
response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'})
df = pd.read_html(response.text)[0][:-1]

My suggestion would be to use pandas_datareader ( conda install pandas-datareader )

import pandas_datareader.data as web
import pandas as pd

df = web.DataReader('BTC-USD', 'yahoo', start='2022-01-01', end='2022-06-14')
df.tail(10)

Result:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM