使用python实现抓取中国银行外汇牌价首页数据实现

作者:BoBo啵啵 时间:2021-11-28 02:05:49 

利用requests、BeautifulSoup、xlwings库抓取中国银行外汇牌价首页数据

1. 利用requests、BeautifulSoup、xlwings库抓取中国银行外汇牌价首页数据。

(1)中国银行外汇牌价网址如下。

https://www.bankofchina.com/sourcedb/whpj/

使用python实现抓取中国银行外汇牌价首页数据实现

(2)调用requests模块中get方法访问上述网址,获取Response 对象。


url = "https://www.bankofchina.com/sourcedb/whpj/"
web=re.get(url)

(3)利用BeautifulSoup类解析。


#BeautifulSoup将字节流转换为utf-8编码
bs_obj=BeautifulSoup(web.text,'lxml')

(4)利用find_all方法查找table、tr、td等标签对象。

#查找数据所在表格


table=bs_obj.find_all('table')[1]
all_th=all_tr.find_all('th')
#print(all_th)
all_td=all_tr.find_all('td')
#print(all_td)

(5)将找到的相应标签内容依次添加到列表中。


if len(all_th)>0:
       dataRow=[]
       for item in all_th:
           dataRow.append(item.text)
       dataAll.extend([dataRow])
   if len(all_td)>0:
       dataRow=[]
       for item in all_td:
           dataRow.append(item.text)
       dataAll.extend([dataRow])

(6)利用xlwings库,将列表内容写入Excel文件。


wb=xw.Book()
sht=wb.sheets('Sheet1')
sht.range('a1').value=dataAll#将数据添加到表格中

(7)利用这部分数据建立折线图。


chart=sht.charts.add(500,50,700,400)
chart.set_source_data(sht.range('A1:A28,C1:C28,E1:E28'))#设置数据画图
chart.chart_type='line_markers'

chart.name='line_markersd'
#chart.api[1].ChartTitle.Text='中国银行外汇牌价'

Code:


import requests as re
from bs4 import BeautifulSoup
import xlwings as xw
url = "https://www.bankofchina.com/sourcedb/whpj/"
web=re.get(url)
web.encoding=web.apparent_encoding
#BeautifulSoup将字节流转换为utf-8编码
bs_obj=BeautifulSoup(web.text,'lxml')
#查找数据所在表格
table=bs_obj.find_all('table')[1]
#print(table)
dataAll=[]
for all_tr in table.find_all('tr'):#找到所有tr,返回一个列表
   all_th=all_tr.find_all('th')
   #print(all_th)
   all_td=all_tr.find_all('td')
   #print(all_td)
   if len(all_th)>0:
       dataRow=[]
       for item in all_th:
           dataRow.append(item.text)
       dataAll.extend([dataRow])
   if len(all_td)>0:
       dataRow=[]
       for item in all_td:
           dataRow.append(item.text)
       dataAll.extend([dataRow])
wb=xw.Book()
sht=wb.sheets('Sheet1')
sht.range('a1').value=dataAll#将数据添加到表格中
chart=sht.charts.add(500,50,700,400)
chart.set_source_data(sht.range('A1:A28,C1:C28,E1:E28'))#设置数据画图
chart.chart_type='line_markers'
chart.name='line_markersd'
#chart.api[1].ChartTitle.Text='中国银行外汇牌价'

使用python实现抓取中国银行外汇牌价首页数据实现

来源:https://blog.csdn.net/gezongbo/article/details/120986228?utm_medium=distribute.pc_feed.none-task-blog-personrec_tag-7.nonecase&depth_1-utm_source=distribute.pc_feed.none-task-blog-personrec_tag-7.nonecase

标签:Python,外汇牌价,数据抓取
0
投稿

猜你喜欢

  • pandas 转换成行列表进行读取与Nan处理的方法

    2021-10-24 14:27:41
  • python报错TypeError: ‘NoneType‘ object is not subscriptable的解决方法

    2023-01-11 08:11:07
  • Discuz!NT 论坛整合ASP程序论坛

    2011-03-31 10:40:00
  • Pytorch学习笔记DCGAN极简入门教程

    2022-05-28 17:29:02
  • Python中免验证跳转到内容页的实例代码

    2021-03-27 11:19:55
  • 在Python3.74+PyCharm2020.1 x64中安装使用Kivy的详细教程

    2023-01-16 15:17:39
  • python paramiko实现ssh远程访问的方法

    2021-07-17 23:03:55
  • Python利用机器学习算法实现垃圾邮件的识别

    2021-02-24 04:31:04
  • 使用Python的turtle模块画国旗

    2021-10-22 12:31:10
  • PyTorch 可视化工具TensorBoard和Visdom

    2022-04-28 00:30:41
  • 使用:after清除浮动

    2008-10-30 12:10:00
  • 全兼容的纯CSS级联菜单要点浅析

    2009-06-10 14:42:00
  • Go中的应用配置管理详解

    2023-06-21 00:40:55
  • Python中设置变量访问权限的方法

    2023-09-13 15:42:15
  • 查找python项目依赖并生成requirements.txt的方法

    2021-11-27 20:41:43
  • Python统计可散列的对象之容器Counter详解

    2023-09-23 18:30:50
  • Django 2.0版本的新特性抢先看!

    2021-01-03 11:10:50
  • 解析:怎样在MySQL中获得更好的搜索结果

    2008-11-27 15:19:00
  • Django数据统计功能count()的使用

    2022-12-21 23:49:29
  • 查看Python依赖包及其版本号信息的方法

    2021-10-13 23:52:59
  • asp之家 网络编程 m.aspxhome.com