吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2946|回复: 32
收起左侧

[Python 原创] 爬取汇率/国债收益率/黄金的价格,PushPlus推送

  [复制链接]
平Fan_d世界 发表于 2023-9-18 09:41
本帖最后由 平Fan_d世界 于 2023-9-18 09:45 编辑

先看效果



代码:

[Python] 纯文本查看 复制代码
import sys
import requests
import numpy as np
from bs4 import BeautifulSoup 

# PushPlus推送模块
def pushplus(_item, _message):
    token = 'xxxxxxxxx'  #填入你的
    api = 'http://www.pushplus.plus/send'
    _d = {
        "token": token,
        "title": _item,
        "content": _message,
        "template": "markdown" #使用markdown格式
    }
    req = requests.post(api, data=_d)

#爬取模块
id=['fx_susdcny','fx_susdcnh','globalbd_gcny10','gds_AUTD']
#gds_AUTD,黄金延期
#globalbd_gcny10,10年国债收益率
#fx_susdcny,在岸人民币
#fx_susdcnh,离岸人民币
np={}
url='http://hq.sinajs.cn/list='
referer='https://finance.sina.com.cn/'
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36 Edg/90.0.818.41','referer': referer}
#headers={'referer':'https://finance.sina.com.cn/'}
i=1
for id in id:
        r=requests.get(url+id,headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        soup=BeautifulSoup(r.text,'html.parser')
        a=soup.string.strip().replace('"',',').split(',')
        np[i]=a
        #print(soup)
        i=i+1
        
#print(np)

#使用markdown格式
num2="{:.2%}".format(float(np[2][9])/float(np[2][4])-1)
desp="> ### **离岸人民币(USDCNH)**"+'\n'+"###### "+np[2][18]+" "+np[2][1]+'\n'+"##### 现价:"+np[2][9]+"  昨收:"+np[2][4]+"  涨幅:"+num2+'\n'+"##### 今开:"+np[2][6]+"  最高:"+np[2][7]+"  最低:"+np[2][8]+'\n'+ "- - - - \n"+"\n"
num1="{:.2%}".format(float(np[1][9])/float(np[1][4])-1)
desp=desp+"> ### **在岸人民币(USDCNY)**"+'\n'+"###### "+np[1][18]+" "+np[1][1]+'\n'+"##### 现价:"+np[1][9]+"  昨收:"+np[1][4]+"  涨幅:"+num1+'\n'+"##### 今开:"+np[1][6]+"  最高:"+np[1][7]+"  最低:"+np[1][8]+'\n'+ "- - - - \n"+"\n"
num3="{:.2%}".format(float(np[3][4])/float(np[3][3])-1)
desp=desp+"> ### **中国10年期国债收益率(GCNY10)**"+'\n'+"###### "+np[3][13]+" "+np[3][14]+'\n'+"##### 现价:"+np[3][4]+"  昨收:"+np[3][3]+"  涨幅:"+num3+'\n'+"##### 今开:"+np[3][2]+"  最高:"+np[3][5]+"  最低:"+np[3][6]+'\n'+ "- - - - \n"+"\n"
num4="{:.2%}".format(float(np[4][1])/float(np[4][8])-1)
desp=desp+"> ### **黄金延期(AUTD)**"+'\n'+"###### "+np[4][13]+" "+np[4][7]+'\n'+"##### 现价:"+np[4][1]+"  昨收:"+np[4][8]+"  涨幅:"+num4+'\n'+"##### 今开:"+np[4][9]+"  最高:"+np[4][5]+"  最低:"+np[4][6]+'\n'+ "- - - - \n"+"\n"

#推送
#print(desp)
pushplus("📈金融资产价格💰",desp)

免费评分

参与人数 6吾爱币 +11 热心值 +6 收起 理由
豆瓣辣酱TM + 1 + 1 热心回复!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
42282287 + 1 我很赞同!
wbzb + 1 + 1 热心回复!
Gyb + 1 我很赞同!
wkdxz + 2 + 1 请问这个是怎么抓到的,楼主可否给个步骤 http://hq.sinajs.cn/list=gds_A.

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

joy95611 发表于 2023-9-18 14:00
本帖最后由 joy95611 于 2023-9-18 14:01 编辑

没有跑通,网络问题?

python ./goldprice.py
Traceback (most recent call last):
  File "/home/xkada284/anaconda3/lib/python3.6/site-packages/urllib3/connection.py", line 175, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "/home/xkada284/anaconda3/lib/python3.6/site-packages/urllib3/util/connection.py", line 72, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/home/xkada284/anaconda3/lib/python3.6/socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Temporary failure in name resolution


During handling of the above exception, another exception occurred:


Traceback (most recent call last):
  File "/home/xkada284/anaconda3/lib/python3.6/site-packages/urllib3/connectionpool.py", line 710, in urlopen
    chunked=chunked,
  File "/home/xkada284/anaconda3/lib/python3.6/site-packages/urllib3/connectionpool.py", line 398, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/home/xkada284/anaconda3/lib/python3.6/site-packages/urllib3/connection.py", line 239, in request
    super(HTTPConnection, self).request(method, url, body=body, headers=headers)
  File "/home/xkada284/anaconda3/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/home/xkada284/anaconda3/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/home/xkada284/anaconda3/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/home/xkada284/anaconda3/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/home/xkada284/anaconda3/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/home/xkada284/anaconda3/lib/python3.6/site-packages/urllib3/connection.py", line 205, in connect
    conn = self._new_conn()
  File "/home/xkada284/anaconda3/lib/python3.6/site-packages/urllib3/connection.py", line 187, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7f0993028ef0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution


During handling of the above exception, another exception occurred:


Traceback (most recent call last):
  File "/home/xkada284/anaconda3/lib/python3.6/site-packages/requests/adapters.py", line 450, in send
    timeout=timeout
  File "/home/xkada284/anaconda3/lib/python3.6/site-packages/urllib3/connectionpool.py", line 786, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "/home/xkada284/anaconda3/lib/python3.6/site-packages/urllib3/util/retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='hq.sinajs.cn', port=80): Max retries exceeded with url: /list=gds_AUTD (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f0993028ef0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))


During handling of the above exception, another exception occurred:


Traceback (most recent call last):
  File "./goldprice.py", line 31, in <module>
    r=requests.get(url+id,headers=headers)
  File "/home/xkada284/anaconda3/lib/python3.6/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/home/xkada284/anaconda3/lib/python3.6/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/xkada284/anaconda3/lib/python3.6/site-packages/requests/sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/xkada284/anaconda3/lib/python3.6/site-packages/requests/sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "/home/xkada284/anaconda3/lib/python3.6/site-packages/requests/adapters.py", line 519, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='hq.sinajs.cn', port=80): Max retries exceeded with url: /list=gds_AUTD (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f0993028ef0>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))
 楼主| 平Fan_d世界 发表于 2023-9-18 10:49
wkdxz 发表于 2023-9-18 10:34
请问这个是怎么抓到的,楼主可否给个步骤  http://hq.sinajs.cn/list=gds_AUTD

http://hq.sinajs.cn/list=代码
新浪https://finance.sina.com.cn/数据源,也能抓股票
爬时候要在headers加https://finance.sina.com.cn/的Referer

页面检查-网络里观察,看看哪个关键词重复出现
海是倒过来的天 发表于 2023-9-18 09:49
吖力锅 发表于 2023-9-18 09:54
我还没学到这个。,亲是自学的 吗?
 楼主| 平Fan_d世界 发表于 2023-9-18 09:57
本帖最后由 平Fan_d世界 于 2023-9-18 09:58 编辑

吖力锅 发表于 2023-9-18 09:54
我还没学到这个。,亲是自学的 吗?

我是新手,在校时学过点其它编程语言,python还没系统的学习。

善用搜索引擎
xiaoysm 发表于 2023-9-18 10:02
学习了,pushplus挺实用的
Stonesun007 发表于 2023-9-18 10:15
pushplus可以发到个人微信上面不?
wkdxz 发表于 2023-9-18 10:34
请问这个是怎么抓到的,楼主可否给个步骤  http://hq.sinajs.cn/list=gds_AUTD
wkdxz 发表于 2023-9-18 11:27
平Fan_d世界 发表于 2023-9-18 10:49
http://hq.sinajs.cn/list=代码
新浪https://finance.sina.com.cn/数据源,也能抓股票
爬时候要在header ...

看到了 谢了兄弟
雾都孤尔 发表于 2023-9-18 12:32
学习了,感谢分享。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 20:47

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表