【发布时间】:2019-12-02 03:53:41
【问题描述】:
我正在尝试从 coinbase 中获取比特币价格,但找不到正确的语法。当我运行程序(不带问号的行)时,我得到了我需要的 html 块,但我不知道如何缩小和检索价格本身。任何帮助表示赞赏,谢谢。
import requests
from bs4 import BeautifulSoup
url = 'https://www.coinbase.com/charts'
data = requests.get(url)
nicedata = data.text
soup = BeautifulSoup(nicedata, 'html.parser')
prettysoup = soup.prettify()
bitcoin = soup.find('h4', {'class':
'Header__StyledHeader-sc-1q6y56a-0 hZxUBM
TextElement__Spacer-sc-18l8wi5-0 hpeTzd'})
price = bitcoin.find('???')
print(price)
【问题讨论】:
-
price = bitcoin.text -
这个类有很多
<h4>-find()只得到第一个,它有文字Bitcoint,而不是你的图片价格。您可能需要find_all()才能将所有项目作为列表。稍后for-loop 检查列表中的每个元素以获得预期值。 -
获取行
<tr>和稍后在行搜索<h4>可能更容易。这样,您可以在列表或 numpy.array 或 pandas.DataFrame 列表中组织数据 -
阅读 BeautifulSoup 文档,它们在 IMO 方面非常出色。您在寻找 BeautifulSoup 的指南吗?
标签: python html css syntax beautifulsoup