Python爬取实时的贵金属行情

本文介绍了如何使用Python的requests和BeautifulSoup库从中国工商银行新一代网上银行抓取贵金属的实时价格信息,包括名称、价格、涨跌幅等数据。
摘要由CSDN通过智能技术生成

 原网址:中国工商银行新一代网上银行 (icbc.com.cn)

通过元素获取到表格对应的链接

开始,写了个字典存对应的中文名称,Python不是我的主力语言,而且很久没写了,所以写的很烂,此代码仅供学习研究,不要用于非法用途,否则后果自负

import requests
from bs4 import BeautifulSoup
import re
from datetime import datetime
# 用于发送请求的URL
url = 'https://mybank.icbc.com.cn/icbc/newperbank/perbank3/gold/futuresgold_query_out.jsp'

headers = {
   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
   'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
   'Cache-Control': 'max-age=0',
   'Connection': 'keep-alive',
   'Sec-Fetch-Dest': 'document',
   'Sec-Fetch-Mode': 'navigate',
   'Sec-Fetch-Site': 'none',
   'Sec-Fetch-User': '?1',
   'Upgrade-Insecure-Requests': '1',
   'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0',
   'sec-ch-ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Microsoft Edge";v="122"',
   'sec-ch-ua-mobile': '?0',
   'sec-ch-ua-platform': '"Windows"',
   'Cookie': 'isP3bank=1; isEn_US=0; isPri=; firstZoneNo=%E5%B9%BF%E4%B8%9C_2000; arialoadData=true; ariawapChangeViewPort=true; CK_ISW_EBANKP_EBANKP-WEB-IPV6-NEW_80=bambfbmbegmbahnia-10|ZgEhV|ZgETe; sg___persistence__sugoio=%7B%22distinct_id%22%3A%20%2218e743d76de1b8-0c13ef4a74592f-4c657b58-186a00-18e743d76df2d63%22%7D'
}

gold_dict = {
    "Au(T+D)": "黄金T+D",
    "mAu(T+D)": "m黄金T+D",
    "Ag(T+D)": "白银T+D",
    "AuT+D": "黄金T+D",
    "mAuT+D": "m黄金T+D",
    "AgT+D": "白银T+D",
    "Au9999": "黄金9999",
    "Au9995": "黄金9995",
    "Au100g": "金条100g",
    "Au50g": "金条50g",
    "Ag999": "白银999",
    "AuT+N1": "黄金T+N1",
    "AuT+N2": "黄金T+N2",
    "SGiAu100g": "gi黄金100g",
    "SGiAu9999i": "i黄金9999",
    "SGiAu995i": "i黄金995",
    "Ag9999": "白银9999",
    "PT9995": "铂金9995",
    "NYAuTN06": "纽约金TN06",
    "NYAuTN12": "纽约金TN12",
}


# 发送请求并获取网页内容
response = requests.get(url)
web_content = response.text

# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(web_content, 'html.parser')

# 根据网页结构提取贵金属价格信息
# 这里假设价格信息在id为'TABLE1'的表格中
table = soup.find('table', {'id': 'resultTable'})
rows = table.find_all('tr')

# print(rows)
# 解析每一行,提取贵金属名称和价格
for i,row in enumerate(rows[1:-1]):  # 跳过表头
    cols = row.find_all('td')
    for x,col in enumerate(cols[:2]+cols[3:]):
        if x==0 :
            print("代码:" + col.text.strip())
            if col.text.strip() in gold_dict:
                print("名称:"+gold_dict[col.text.strip()])
        elif x==1 :
            print("当前价:" + col.text.strip())
        elif x==2 :
            print("涨跌幅:" + col.text.strip())
        elif x==3 :
            print("成交量(手):" + col.text.strip())
        elif x==4 :
            print("开盘价:" + col.text.strip())
        elif x==5 :
            print("昨收价:" + col.text.strip())
        elif x==6 :
            print("最高价:" + col.text.strip())
        elif x==7 :
            print("最低价:" + col.text.strip())
    print("\n")
        # print(col.text.strip())
        
time_element = soup.find('td', {'id': 'time'})
time_str = re.search(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}', time_element.text).group()
# print(time_str)
update_time = datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
formatted_time = update_time.strftime('%Y年%m月%d日 %H:%M:%S')
print("截止时间:"+formatted_time)

运行结果:

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zzl_Kevin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值