并存放在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()