简体   繁体   English

使用Python(Pycharm)从Coinbase进行网络爬取

[英]Web-scraping from Coinbase with Python (Pycharm)

I am trying to write some code that will give me the price in BTC when I run it. 我正在尝试编写一些代码,这些代码可以在运行时为我提供BTC的价格。 Although I am not getting an error after running the code, I am not getting the price and I am getting NONE . 尽管在运行代码后没有出现错误,但是没有得到价格,也没有得到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)

Thanks a lot! 非常感谢!

Data that you want to scrape is dinamycally generated. 您要抓取的数据会自动生成。 You can make direct request to API to get those values: 您可以直接向API请求以获取这些值:

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

Output: 输出:

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

To get required value: 要获得所需的值:

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

Output: 输出:

'7590.01'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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