python爬虫,银行数据

import requests
from bs4 import BeautifulSoup
import re
import xlwt
finddata=re.compile(r'黄金价格.*收于(.*)美元/盎司;白银价格.*收于(.*)美元/盎司。')
fandlink=re.compile(r'href="(.*)" target="_blank">')
findday=re.compile(r'(中国工商银行金融市场部 (.*))</span>')

url1='http://www.icbc.com.cn/icbc/%e7%bd%91%e4%b8%8a%e9%bb%84%e9%87%91/%e8%b4%b5%e9%87%91%e5%b1%9e%e8%b5%84%e8%ae%af/%e5%b8%82%e5%9c%ba%e5%bf%ab%e6%8a%a5/default.htm'
url3='http://www.icbc.com.cn/icbc/%e7%bd%91%e4%b8%8a%e9%bb%84%e9%87%91/%e8%b4%b5%e9%87%91%e5%b1%9e%e8%b5%84%e8%ae%af/%e5%b8%82%e5%9c%ba%e5%bf%ab%e6%8a%a5/default-PageList-'
headers ={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.47'}

def getdata(url2):
    r = requests.get(url=url2, headers=headers)
    r.encoding = 'utf-8'
    bs = BeautifulSoup(r.text, 'html.parser')
    item = bs.find_all('span', id='MyFreeTemplateUserControl')
    item2=bs.find_all('span',id="InfoPickFromFieldControl")
    item = str(item)
    item2=str(item2)
    data = re.findall(finddata, item)
    day = re.findall(findday,item2)
    return data[0][1],data[0][0],day[0]


z=str(3)
y='.htm'

worlbook=xlwt .Workbook (encoding= 'utf-8')#创建一个文件
worksheet=worlbook .add_sheet('sheet2')#创建一个文件的表单
col=['黄金','白银','日期']
for i in range (3):
    worksheet .write(0,i,col[i])

k=1
for x in range(4):
    if x==0:
        url4=url1
    else:
        url4=url3+z+y
        z = str(int(z) - 1)
    r=requests.get(url=url4,headers =headers )
    r.encoding='utf-8'
    bs=BeautifulSoup (r.text  ,'html.parser')

    for i in range(1):
        for item in bs.find_all('span',class_="ChannelSummaryList-insty"):
            item=str(item)
            x=re.findall(fandlink,item)
            url2='http://www.icbc.com.cn'+x[0]
            ag,gd,day=getdata(url2)
            # print(f'日期{day}')
            # print(f'白银{ag}')
            # print(f'黄金{gd}')
            worksheet .write (k,0,gd)
            worksheet .write (k,1,ag)
            worksheet .write (k,2,day)
            k=k+1

worlbook.save('date2.xls')
print('爬取完毕!')

#以下是我自己写的画图代码

import matplotlib as mpl
import numpy as np
import pandas as pd
import matplotlib .pyplot as plt
import matplotlib
cf=pd.read_excel ('date2.xls')
#print(cf)
ga=cf.loc [:,['黄金','日期','白银']]
#print(ga)
# #将日期转化为时间序列
ga['日期']=pd.to_datetime(cf['日期'])
#将日期升序排列
ga=ga.set_index('日期').sort_index(ascending=True )
#print(ga)
#
zhfont1 = matplotlib.font_manager.FontProperties(fname="SourceHanSansSC-Bold.otf", size=18)
y1=ga['黄金']
x1=range(len(y1))
y2=ga['白银']
x2=range(len(y2))
plt.figure(1)
plt.subplot(221)
plt.plot(x1,y1,'r-')
ypoints = np.array([1859.07075,1859.07075]*40)
plt.plot(ypoints, linestyle = 'dotted',color = 'black')
plt.xlabel("time")
plt.ylabel("ug")
plt.subplot(224)
plt.plot(x2,y2,'b-')
ypoints = np.array([23.842875,23.842875]*40)
plt.plot(ypoints, linestyle = 'dotted',color = 'black')
plt.xlabel("time")
plt.ylabel("ag")
plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值