网络爬虫丨基于requests爬取比特币信息并绘制价格走势图
本期内容:基于requests爬取比特币信息并绘制价格走势图
current period : requests climb bitcoin information and map price trends
下载地址:https://download.csdn.net/download/m0_68111267/88734451
- anaconda丨pycharm
- python3.11.4
- requests
安装requests库的命令:
The order to install the réquests libraryxff1a;
- 学习网络爬虫相关技术,熟悉爬虫基本库requests的使用。
- 熟悉网络爬虫相关基础知识。
1. 尝试爬取网站数据
1. Try to crawl to web data
编写实例文件"bt01.py", 抓取网站中BTC和ETH的报价数据并打印输出BTC和ETH的实时价格:
Preparation of example documents 34; bt01.py" xff0c; capture of quoted data from BTC and ETH on the website and printing of real-time prices for BTC and ETH outputxff1a;
程序设计
program design
程序分析
program analysis
这段代码使用了Python的requests库来获取加密货币的价格信息。它定义了一个名为price的函数,该函数接受一个代币符号作为参数,并可选地接受比较货币符号列表和交易所名称。
This code uses Python's réquests library to obtain price information for encrypted currencies. It defines a function called Price & #xff0c; the function accepts a token symbol as parameter & #xff0c; and the list of comparative currency symbols and the name of the exchange can be selected.
函数的逻辑是先构建一个url,然后使用requests库发送GET请求获取到加密货币的价格数据。该url使用了Cryptocompare的API,通过拼接符号和比较货币来构建特定的API请求。
The logic of the function is to build an url& #xff0c first; then use the réquests library to send GET requests for price data for encrypted currencies. The url uses Cryptocompare's API, constructs specific API requests by collating symbols and comparing currencies.
在主函数中,它使用price函数分别获取了BTC和ETH的美元价格,然后将结果打印出来。
In the main function xff0c; it uses the price function to obtain the dollar price of BTC and ETH respectively xff0c; and then prints the results out.
要对这段代码进行详细分析,可以从以下几个方面入手:
The code is to be analysed in detail xff0c; this can be done in the following ways xff1a;
-
函数的参数和返回值:函数接受一个代币符号作为参数,可选地接受比较货币符号列表和交易所名称。它返回一个包含价格信息的字典对象。
function & #xff1a; the function accepts a token symbol as a parameter & #xff0c; optionally accepts the list of comparative currency symbols and the name of the exchange. It returns a dictionary object that contains price information.
-
API请求:函数构建了一个特定的API请求url,然后使用requests库发送GET请求获取到加密货币的价格数据。这个请求使用了Cryptocompare的API,可以根据代币符号和比较货币获取实时价格信息。
API requests & #xff1a; the function constructs a specific API request for url, then uses the réquests library to send GET requests for price data for encrypted currencies. This request uses Cryptocompare's API, real-time price information can be obtained on the basis of a token and a comparative currency.
-
数据解析和处理:函数获取到API返回的数据后,使用json()方法将其转换为Python字典对象。然后,将价格信息提取出来并返回。
Data parsing and processing xff1a; the function retrieves the API returned data xff0c; converts it to a Python dictionary object using a json() method. Then xff0c; extracts the price information and returns it.
-
主函数的使用:主函数调用了price函数来获取BTC和ETH的美元价格,并将结果打印出来。
The main function uses xff1a; the main function calls the price function to obtain the dollar price of BTC and ETHxff0c; and prints out the results.
总体来说,这段代码是一个简单的加密货币价格获取程序。它使用了Cryptocompare的API来获取实时价格数据,并将结果返回。可以根据需要进一步扩展和修改该代码,以适应更多的加密货币和交易所。
In general, xff0c; this code is a simple encrypt currency price acquisition program. It uses Cryptocompare's API to capture real-time price data xff0c; and returns the result. The code xff0c can be further expanded and modified; it can be adapted to more encrypted currencies and exchanges.
运行结果
2. 简单绘制一下比特/美元价格曲线
2. A simple bit/dollar price curve
编写实例文件"bt02.py", 使用matplotlib库绘制比特/美元的价格曲线:
Preparation of example documents 34; bt02.py" xff0c; mapping of price curves of bits/dollars xff1a using the matplatlib library;
程序设计
program design
程序分析
program analysis
这段代码是一个用于实时绘制比特币价格走势图的程序。它导入了一个名为price的函数,该函数在之前的代码中已经定义了,用于获取比特币的价格信息。同时,它还导入了一些必要的库,例如datetime和matplotlib.pyplot。
This code is a program for real-time mapping of bitcoin prices. It imports a function called Price xff0c; this function has defined xff0c in the previous code; it is used to get price information for bitcoin. xff0c; it also imports some of the necessary libraries xff0c; e.g. datetime and matplatlib.pyplot.
代码中定义了两个空列表x和y,用于存储时间和比特币价格。接下来,创建一个图形容器fig,并使用plt.gcf()方法初始化。然后,显示图形容器fig,并通过fig.canvas.draw()绘制空图。
The code defines two empty lists x and y, used for storage time and bitcoin prices. Next & #xff0c; created a graphic packaging fig, and initialized using the plt.gcf() method. & #xff0c; displays the graphic packaging fig, and makes empty maps via fig.canvas.draw().
使用一个while循环不断获取比特币的价格信息,并像之前一样调用price函数。然后,将时间和价格分别添加到x和y列表中。接下来,通过plt.ylim()设置y轴的范围,plt.title()设置图表标题,并使用plt.plot()方法绘制x和y的曲线。最后,使用fig.canvas.draw()绘制更新后的图形,并使用plt.pause()方法暂停10秒钟。
& #xff0c. Then & #xff0c; add time and price to the list of x and y, respectively. Next & #xff0c; set the range of the y-axis by plt.ylim() & #xff0c; plt.title() sets the chart title xff0c; and draws the x and y curves by plt.pllot() method. Final xff0c; draw the updated graphics xff0c using fig.canvas.draw(); and hold the plt.pause() method for 10 seconds.
总体来说,这段代码可以不断获取比特币的实时价格,并以折线图的形式展示出来。通过不断更新图形容器,可以实时查看比特币价格的变化。可以根据需要调整代码中的参数,以适应不同的需求和交易所。
In general, xff0c; this code allows the acquisition of real-time bitcoin prices xff0c; it is displayed in the form of line charts. It allows real-time viewing of bitcoin price changes by constantly updating graphic packagings xff0c. The parameters in the code xff0c can be adjusted according to need; it can be adapted to different needs and exchanges.
运行结果
3. 绘制BTC和ETH的历史价格曲线图
3. Draw historical price curves for BTC and ETH
编写实例文件"bt03.py", 首先抓取网站中BTC和ETH的历史价格数据,然后使用matplotlib和pandas绘制BTC和ETH的历史价格曲线图:
Preparation of example documents 34;
程序设计
program design
程序分析
program analysis
这段代码是一个获取比特币和以太坊近期小时价格历史数据,并绘制折线图的程序。
This code is a procedure for obtaining historical data on bitcoin and near-term prices in Etheria xff0c; and for drawing line charts.
首先,代码中定义了两个函数,hourly_price_historical和plotchart。hourly_price_historical函数用于获取指定货币对的历史小时价格数据,它接收参数symbol(货币符号)、comparison_symbol(对比货币符号)、limit(获取的小时数量)、aggregate(聚合参数,指定每个数据点的时间间隔)以及exchange(交易所,可选)。该函数首先构建了API请求的URL,并发送请求获取数据。然后,使用pandas库将返回的数据转换为DataFrame格式,并添加一个timestamp列,表示时间。最后,返回DataFrame。
First xff0c; the code defines two functions xff0c; hourly_price_historical and plotchart. hourly_price_historical xff0c; it receives parameters symbol( currency symbol xff09; comparson_symbol( comparison currency symbol xff09; mitxff08; number of hours taken xff09; aggregregate( aggregation parameter xff0c; specify time intervals of each data point xff09; and exchangexff08; exchange ffff0; option #xffff09; first to return the URL #xffcc requested by API to request data; and then to send data requests.
plotchart函数用于绘制折线图,它接收参数axis(子图坐标轴)、df(包含价格数据的DataFrame)、symbol(货币符号)和comparison_symbol(对比货币符号)。该函数使用plot方法绘制数据中的close列(表示收盘价)与时间戳列(timestamp)。
The plotchart function is used to draw line charts & #xff0c; it receives parameters axis( subchart axis & #xff09; df( DataFrame) symbol( currency symbol xff09; and comparison_symbol( comparative currency symbol xff09; the function uses the plot method to draw data for close column #xff08; represents collection price #xff09; and time stamp (tamestamp).
在主程序中,首先调用hourly_price_historical函数获取比特币和以太坊的历史小时价格数据,分别存储在df1和df2中。然后,使用plt.subplots()创建一个包含两个子图的图形。接下来,调用plotchart函数分别绘制比特币和以太坊的折线图,并传入对应的数据和货币对信息。最后,调用plt.show()显示图形。
xff0c in the main program; first call the hour_price_historical function to get Bitcoin and Xff0c historical hour price data xff0c; stored in df1 and df2 respectively. Then , create a graphic with two subcharts using plt.subplots() xff0c; then call the platchart function to draw bitcoin and xff0c; and pass on the corresponding data and currency pair information. Last #xff0c; call plt.show() to display graphics.
总体来说,这段代码可以获取指定货币对的历史小时价格数据,并以折线图的形式展示出来。通过使用pandas和matplotlib库,可以方便地处理和可视化数据,以便进行技术分析和趋势观察。可以根据需要调整代码中的参数,以获取不同货币对的历史价格数据。
In general, xff0c; this code allows for the acquisition of historical hour price data for specified currencies xff0c; and it is displayed in the form of line charts. The historical price data for different currencies can be adjusted according to the parameters of the code xff0c; by using the pandas and matpoltlib library xff0c; by easily processing and visualizing data xff0c; for technical analysis and trend observations.
运行结果
我是一只有趣的兔子,感谢你的喜欢!
{\bord0\shad0\alphaH3D}I'm an interesting rabbit.
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。