import time
import requests
import json
class EastMoney(object):
def __init__(self):
# 套查詢的頁碼
self.page = 1
# 允許每頁顯示的數據條數
self.count = 3820
# 當前向服務器發起請求的時間戳11位,毫秒級
self.ms = int(round(time.time() * 1000))
# 數據保存地址
self.path = "G:\\個人總結\\csv\\"
# 封裝的URL
self.url = "http://17.push2.eastmoney.com/api/qt/clist/get?cb=jQuery1124011866178211308975_1569309447330&pn={0}&pz={1}&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:0+t:6,m:0+t:13,m:0+t:80,m:1+t:2,m:1+t:23&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f22,f11,f62,f128,f136,f115,f152&_={2}".format(
self.page, self.count, self.ms
)
# 請求頭
self.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
}
def getResponse(self):
# 服務器響應數據json加載爲python對象
return json.loads(requests.get(self.url, headers=self.headers).text.split("(")[1].replace(");", ""))
def save(self, content):
with open(self.path + "eastmoney.csv", "a+", encoding="UTF-8") as file:
file.write(str(content) + "\n")
def parse(self):
data = self.getResponse()
for index, item in enumerate(data["data"]["diff"]):
product = {
"代碼": item["f12"] + " " + "http://quote.eastmoney.com/sz{}.html".format(item["f12"]),
"名稱": item["f14"] + " " + "http://quote.eastmoney.com/sz{}.html".format(item["f12"]),
"股吧": "http://guba.eastmoney.com/list,{}.html".format(item["f12"]),
"資金流": "http://data.eastmoney.com/zjlx/{}.html".format(item["f12"]),
"數據": "http://data.eastmoney.com/stockdata/{}.html".format(item["f12"]),
"最新價": item["f2"],
"漲跌幅": item["f3"],
"漲跌額": item["f4"],
"成交量(手)": item["f5"],
"成交額": item["f6"],
"振幅": str(item["f7"]) + "%",
"最高": item["f15"],
"最低": item["f16"],
"今開": item["f17"],
"昨收": item["f18"],
"換手率": str(item["f8"]) + "%",
"市盈率(動態)": item["f9"],
"市淨率": item["f23"]
}
print(index, product)
self.save(str(index) + " " + str(product))
if __name__ == "__main__":
EastMoney().parse()