Skip to content

Commit

Permalink
fix: fix futures_spot_stock
Browse files Browse the repository at this point in the history
  • Loading branch information
albertandking committed Mar 21, 2024
1 parent 5d3b589 commit 38dfaba
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions akshare/futures/futures_spot_stock_em.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2022/10/14 17:15
Date: 2024/3/21 12:00
Desc: 东方财富网-数据中心-现货与股票
http://data.eastmoney.com/ifdata/xhgp.html
https://data.eastmoney.com/ifdata/xhgp.html
"""

import pandas as pd
import requests

Expand All @@ -14,7 +15,7 @@
def futures_spot_stock(symbol: str = "能源") -> pd.DataFrame:
"""
东方财富网-数据中心-现货与股票
http://data.eastmoney.com/ifdata/xhgp.html
https://data.eastmoney.com/ifdata/xhgp.html
:param symbol: choice of {'能源', '化工', '塑料', '纺织', '有色', '钢铁', '建材', '农副'}
:type symbol: str
:return: 现货与股票上下游对应数据
Expand All @@ -30,23 +31,25 @@ def futures_spot_stock(symbol: str = "能源") -> pd.DataFrame:
"建材": 6,
"农副": 7,
}
url = "http://data.eastmoney.com/ifdata/xhgp.html"
url = "https://data.eastmoney.com/ifdata/xhgp.html"
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,"
"image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Host": "data.eastmoney.com",
"Pragma": "no-cache",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
}
r = requests.get(url, headers=headers)
data_text = r.text
temp_json = demjson.decode(
data_text[
data_text.find("pagedata"): data_text.find(
data_text.find("pagedata") : data_text.find(
"/newstatic/js/common/emdataview.js"
)
]
Expand Down Expand Up @@ -79,16 +82,19 @@ def futures_spot_stock(symbol: str = "能源") -> pd.DataFrame:
"生产商",
"下游用户",
]
temp_df[date_list[0]] = pd.to_numeric(temp_df[date_list[0]])
temp_df[date_list[1]] = pd.to_numeric(temp_df[date_list[1]])
temp_df[date_list[2]] = pd.to_numeric(temp_df[date_list[2]])
temp_df[date_list[3]] = pd.to_numeric(temp_df[date_list[3]])
temp_df['最新价格'] = pd.to_numeric(temp_df['最新价格'])
temp_df['近半年涨跌幅'] = pd.to_numeric(temp_df['近半年涨跌幅'])
temp_df[date_list[0]] = pd.to_numeric(temp_df[date_list[0]], errors="coerce")
temp_df[date_list[1]] = pd.to_numeric(temp_df[date_list[1]], errors="coerce")
temp_df[date_list[2]] = pd.to_numeric(temp_df[date_list[2]], errors="coerce")
temp_df[date_list[3]] = pd.to_numeric(temp_df[date_list[3]], errors="coerce")
temp_df["最新价格"] = pd.to_numeric(temp_df["最新价格"], errors="coerce")
temp_df["近半年涨跌幅"] = pd.to_numeric(temp_df["近半年涨跌幅"], errors="coerce")
return temp_df


if __name__ == "__main__":
futures_spot_stock_df = futures_spot_stock(symbol="能源")
print(futures_spot_stock_df)

for sector in ["能源", "化工", "塑料", "纺织", "有色", "钢铁", "建材", "农副"]:
futures_spot_stock_df = futures_spot_stock(symbol=sector)
print(futures_spot_stock_df)

0 comments on commit 38dfaba

Please sign in to comment.