爬取汇率/国债收益率/黄金的价格,PushPlus推送

查看 210|回复 10
作者:平Fan_d世界   

先看效果



代码:

[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=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)

收益率, 现价

joy95611   

没有跑通,网络问题?
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: [u]: 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('[u]: 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
    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('[u]: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))
平Fan_d世界
OP
  


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

页面检查-网络里观察,看看哪个关键词重复出现
海是倒过来的天   

学习了,pushplus确实比较 好用
吖力锅   

我还没学到这个。,亲是自学的 吗?
平Fan_d世界
OP
  


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

我是新手,在校时学过点其它编程语言,python还没系统的学习。
善用搜索引擎
xiaoysm   

学习了,pushplus挺实用的
Stonesun007   

pushplus可以发到个人微信上面不?
fish820   

感谢分享
wkdxz   

请问这个是怎么抓到的,楼主可否给个步骤  http://hq.sinajs.cn/list=gds_AUTD
您需要登录后才可以回帖 登录 | 立即注册