python - 散景仅绘制网络抓取数据中的特定列
问题描述
下面的代码从两个不同的网站提取数据,Bokeh 绘制了数据。问题是当我在 x 轴上针对 datatime.now() 绘制“Volumes”数据时,它会被绘制,但是当我绘制“Open”或“Lasts”数据时,我的 Bokeh 图是空白的。我认为抓取的数据包含垃圾字符,但如果是这样,那么卷也不应该被绘制。“ugaz”和“dgaz”也会出现此问题,其中输出导出到 tags1.text 和 tags2.text。我试图理解它已经有一段时间了。
但是,当我 print(source.data) 输出为
{'x': [datetime.datetime(2020, 1, 5, 21, 15, 38, 712611)], 'y': ['1094'], 'y1': ['2.095']}
代码是:
import requests
from bs4 import BeautifulSoup
from bokeh.models import Range1d, LinearAxis
import time
from datetime import datetime
from bokeh.models import ColumnDataSource, DatetimeTickFormatter
from bokeh.plotting import figure, show
from math import radians
import pandas as pd
p = figure()
Volumes = []
Opens = []
Lasts=[]
Contracts =[]
Lows = []
Highs = []
res3 = requests.get('https://shared.websol.barchart.com/quotes/quote.php?
page=quote&sym=ng&x=13&y=8&domain=if&display_ice=1&enabled_ice_exchanges=&tz=0&ed=0')
res1 = requests.get('https://finance.yahoo.com/quote/ugaz?ltr=1')
res2 = requests.get('https://finance.yahoo.com/quote/dgaz?ltr=1')
soup1 = BeautifulSoup(res1.text,'html.parser')
soup2 = BeautifulSoup(res2.text,'html.parser')
tags1 = soup1.find_all('span')[11]
tags2 = soup2.find_all('span')[11]
soup3 = BeautifulSoup(res3.text, 'lxml')
soup3.prettify()
data_rows = soup3.findAll('tr')[2:]
i = range(len(data_rows))
for td in data_rows:
Volume = td.findAll('td')[6].text
Volumes.append(Volume)
Open = td.findAll('td')[3].text
Opens.append(Open)
Last = td.findAll('td')[1].text
Lasts.append(Last)
Contract = td.findAll('td')[0].text
Contracts.append(Contract)
Low = td.findAll('td')[5].text
Lows.append(Low)
High = td.findAll('td')[4].text
Highs.append(High)
source = ColumnDataSource(dict(x=[datetime.now()],y=[Volumes[2]], y1=[Opens[2]]))
p.circle(x ='x', y ='y',source=source,color='blue')
p.circle(x ='x', y ='y1',source=source,color='red')
show(p)
解决方案
您没有将所有值转换为数字:
{
'x': [datetime.datetime(2020, 1, 5, 21, 15, 38, 712611)],
'y': ['1094'], # value in list is a string -- BAD
'y1': ['2.095'] # value in list is a string -- BAD
}
除非您专门绘制分类值(此处不是这种情况),否则 CDS 列中的值通常应始终为数字。
您可以通过调用它们的内置float
函数将字符串转换为数字。
推荐阅读
- amazon-web-services - 对 AWS 弹性 beantalk 强制执行 AES-256
- angularjs - AngularJS轮播具有多个项目和UN等宽
- django - 如何使用 django ORM 删除具有相同值的结果?
- javascript - 无法将 formGroup 与 firebase 错误绑定
- linux - 如何在 PSFTP 中使用 MPUT 来放置具有 777 权限的文件
- c - 如何使用socket可以扩展框架?
- typescript - typescript 静态类型检查 ES 模块导出
- javascript - 如何合并列表中 3 个不同对象的字符串?
- rest - FHIR 服务返回的字符串显示在变量中并且具有长度但在日志记录中不打印(或打印为空格)
- database - PostgreSQL 中的循环外键