每天读取当天东方财富的分红派息数据

并存放在CC.TXT文件里

完整版:

import requests
import time
from bs4 import BeautifulSoup

d1 = time.strftime("%Y-%m-%d", time.localtime())
url = ('http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=GSRL&sty=GSRL&stat=8&fd=666&p=1&ps=200000&js=({pages:(pc),data:[(x)]})&cb=callback&callback=callback&_=')
url_new = url.replace('666',d1)
res = requests.get(url_new)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text,'html.parser')
soup1 = (soup.text.lstrip('callback({pages:1,data:[').rstrip(']})'))


s1 = soup1.replace('"','')
s2 = s1.replace('},{','\n')
print(s2)

# 输出到文本
doc = open('E:\批处理\分红转增\cc.txt','w')
print(s2,file=doc)
doc.close()
之前写的版本:


import requests
import time
from bs4 import BeautifulSoup
d1 = time.strftime("%Y-%m-%d", time.localtime()) #配置带-符号的日期格式,这个在下面的网址中要用到
url = ('http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=GSRL&sty=GSRL&stat=8&fd=666&p=1&ps=200000&js=({pages:(pc),data:[(x)]})&cb=callback&callback=callback&_=')
url_new = url.replace('666',d1) #用时间替代我放在网址中的符号666,来实现每天更新日期
res = requests.get(url_new) #用requests来读取网址,并存放在resres.encoding = 'utf-8' #定义网站的编码soup = BeautifulSoup(res.text,'html.parser') #用beautifulsoup来封装res数据,用html.parser防止代码溢出soup1 = (soup.text.lstrip('callback({pages:1,data:[').rstrip(']})')) #用strip去除左右多余数据s1 = soup1.replace('"','') #消除“s2 = s1.replace('},{','\n') #将‘},{’替换成回车,让一行数据分成多行 

print(s2)# 输出到文本doc = open('cc.txt','w')print(s2,file=doc)doc.close()
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值