使用Python(Pycharm)从Coinbase进行网页爬取 [英] Web-scraping from Coinbase with Python (Pycharm)

查看:175
本文介绍了使用Python(Pycharm)从Coinbase进行网页爬取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一些代码,这些代码可以在运行时为我提供BTC的价格.尽管我在运行代码后遇到错误,但我没有得到价格,并且得到了 NONE .谁能看看我的代码,找出问题所在?这是下面的代码:

I am trying to write some code that will give me the price in BTC when I run it. Although I am not getting an error after running the code, I am not getting the price and I am getting NONE. Can anyone look at my code and figure out what the problem is? Here is the code below:

import requests
from bs4 import BeautifulSoup

page = requests.get("https://www.coinbase.com/charts")
soup = BeautifulSoup(page.content, 'html.parser')
seven_day = soup.find(id="seven-day-forecast")
bitcoin = soup.find('pre',{'style':'word-wrap: break-word; white-space: pre-
wrap;'})

print(bitcoin)

非常感谢!

推荐答案

要抓取的数据是动态生成的.您可以直接向API请求以获取这些值:

Data that you want to scrape is dinamycally generated. You can make direct request to API to get those values:

url = 'https://api.coinbase.com/v2/prices/USD/spot?'
response = requests.get(url).json()
print(response)

输出:

{'data': [{'currency': 'USD', 'base': 'BTC', 'amount': '7590.01'}, {'currency':
'USD', 'base': 'ETH', 'amount': '296.86'}, {'currency': 'USD', 'base': 'LTC', 'amount': '54.59'}]}

要获取所需的值:

print(response['data'][0]['amount'])

输出:

'7590.01'

这篇关于使用Python(Pycharm)从Coinbase进行网页爬取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆