Skip to content

Commit

Permalink
Date (#4205)
Browse files Browse the repository at this point in the history
* add version

* fix index_weibo_sina

* update date
  • Loading branch information
albertandking authored Oct 20, 2023
1 parent 3ad4997 commit e2f6727
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 108 deletions.
5 changes: 3 additions & 2 deletions akshare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2554,9 +2554,10 @@
1.11.38 fix: fix setup.py interface
1.11.39 fix: fix index_option_50etf_qvix interface
1.11.40 fix: fix index_option_300etf_qvix interface
1.11.41 fix: fix index_weibo_sina interface
"""

__version__ = "1.11.40"
__version__ = "1.11.41"
__author__ = "AKFamily"

import sys
Expand Down Expand Up @@ -4506,7 +4507,7 @@
"""
微博指数
"""
from akshare.index.index_weibo import weibo_index
from akshare.index.index_weibo_sina import index_weibo_sina

"""
经济政策不确定性指数
Expand Down
78 changes: 0 additions & 78 deletions akshare/index/index_weibo.py

This file was deleted.

114 changes: 114 additions & 0 deletions akshare/index/index_weibo_sina.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Date: 2023/10/20 16:30
Desc: 微博指数
https://data.weibo.com/
"""
import datetime
import re

import pandas as pd
import requests

from akshare.index.cons import index_weibo_headers


def _get_items(symbol: str = "股票") -> dict:
"""
新浪微博-名词编码
https://data.weibo.com/
:param symbol: 需要查询的名词
:type symbol: str
:return: 名词编码
:rtype: dict
"""
url = "https://data.weibo.com/index/ajax/newindex/searchword"
payload = {"word": symbol}
r = requests.post(url, data=payload, headers=index_weibo_headers)
temp_dict = {symbol: re.findall(pattern=r"\d+", string=r.json()["html"])[0]}
return temp_dict


def _get_index_data(wid: str = "", time_type: str = "") -> pd.DataFrame:
"""
新浪微博-微博指数数据
https://data.weibo.com/
:param wid: 名词编码
:type wid: str
:param time_type: 时间区间
:type time_type: str
:return: 微博指数数据
:rtype: pandas.DataFrame
"""
url = "http://data.weibo.com/index/ajax/newindex/getchartdata"
data = {
"wid": wid,
"dateGroup": time_type,
}
r = requests.get(url, params=data, headers=index_weibo_headers)
data_json = r.json()
data = {
"index": data_json["data"][0]["trend"]["x"],
"value": data_json["data"][0]["trend"]["s"],
}
temp_df = pd.DataFrame(data)
return temp_df


def _process_index(index: str = "") -> str:
"""
新浪微博-微博指数日期处理
https://data.weibo.com/
:param index: 日期数据
:type index: str
:return: 处理后的日期数据
:rtype: str
"""
now = datetime.datetime.now()
curr_year = now.year
curr_date = "%04d%02d%02d" % (now.year, now.month, now.day)
if "月" in index:
tmp = index.replace("日", "").split("月")
date = "%04d%02d%02d" % (curr_year, int(tmp[0]), int(tmp[1]))
if date > curr_date:
date = "%04d%02d%02d" % (curr_year - 1, int(tmp[0]), int(tmp[1]))
return date
return index


def index_weibo_sina(symbol: str = "python", period: str = "3month") -> pd.DataFrame:
"""
新浪微博-微博指数
https://data.weibo.com/index/newindex
:param symbol: 需要查询的名词
:type symbol: str
:param period: 时间段; choice of {'1hour', '1day', '1month', '3month'}
:type period: str
:return: 微博指数
:rtype: pandas.DataFrame
"""
dict_keyword = _get_items(symbol)
df_list = []
for keyword, wid in dict_keyword.items():
df = _get_index_data(wid, period)
if df is not None:
df.columns = ["index", keyword]
df["index"] = df["index"].apply(lambda x: _process_index(x))
df.set_index("index", inplace=True)
df_list.append(df)
if len(df_list) > 0:
df = pd.concat(df_list, axis=1)
if period == "1hour" or "1day":
df.index = pd.to_datetime(df.index, format="mixed")
else:
df.index = pd.to_datetime(df.index, format="mixed")
df.reset_index(inplace=True)
df.columns = ["datetime", "value"]
df["datetime"] = df["datetime"].astype(str)
return df


if __name__ == "__main__":
index_weibo_sina_df = index_weibo_sina(symbol="股票", period="3month")
print(index_weibo_sina_df)
7 changes: 7 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

| AKShare 版本 | 旧接口名称 | 新接口名称 | 修改日期 |
|------------|---------------------------------------------|---------------------------------------------|----------|
| 1.11.41 | weibo_index | index_weibo_sina | 20231020 |
| 1.11.39 | option_300etf_min_qvix | index_option_300etf_min_qvix | 20231019 |
| 1.11.39 | option_300etf_qvix | index_option_300etf_qvix | 20231019 |
| 1.11.39 | option_50etf_min_qvix | index_option_50etf_min_qvix | 20231019 |
Expand Down Expand Up @@ -67,6 +68,10 @@

## 更新说明详情

1.11.41 fix: fix index_weibo_sina interface

1. 重命名 weibo_index 为 index_weibo_sina 接口

1.11.40 fix: fix index_option_300etf_qvix interface

1. 新增 index_option_300etf_qvix 接口
Expand Down Expand Up @@ -2999,6 +3004,8 @@

## 版本更新说明

1.11.41 fix: fix index_weibo_sina interface

1.11.40 fix: fix index_option_300etf_qvix interface

1.11.39 fix: fix index_option_50etf_qvix interface
Expand Down
52 changes: 26 additions & 26 deletions docs/data/index/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2165,53 +2165,53 @@ print(index_option_300etf_min_qvix_df)
[239 rows x 2 columns]
```

### 微博指数数据
### 新浪微博指数

接口: weibo_index
接口: index_weibo_sina

目标地址: https://data.weibo.com/index/newindex

描述: 获取指定 **word** 的微博指数
描述: 获取指定 symbol 和 period 的微博指数数据

输入参数

| 名称 | 类型 | 描述 |
|-----------|-----|----------------------------------------------------|
| word | str | word="股票" |
| time_type | str | time_type="1hour"; 1hour, 1day, 1month, 3month 选其一 |
| 名称 | 类型 | 描述 |
|--------|-----|-----------------------------------------------------------------|
| symbol | str | symbol="股票"; 需要搜索的关键词 |
| period | str | period="1hour"; choice of {'1hour', '1day', '1month', '3month'} |

输出参数

| 名称 | 类型 | 描述 |
|-------|----------|-------|
| date | datetime | 日期-索引 |
| index | float | 指数 |
| 名称 | 类型 | 描述 |
|----------|---------|------|
| datetime | object | 日期日期 |
| value | float64 | 指数 |

接口示例

```python
import akshare as ak

weibo_index_df = ak.weibo_index(word="期货", time_type="3month")
print(weibo_index_df)
index_weibo_sina_df = ak.index_weibo_sina(symbol="股票", period="3month")
print(index_weibo_sina_df)
```

数据示例

```
期货
index
20190901 13334
20190902 46214
20190903 49017
20190904 53229
20190905 68506
...
20191127 68081
20191128 42348
20191129 62141
20191130 23448
20191201 16169
datetime value
0 2023-07-19 385427
1 2023-07-20 419670
2 2023-07-21 304118
3 2023-07-22 202934
4 2023-07-23 249032
.. ... ...
88 2023-10-15 137029
89 2023-10-16 394977
90 2023-10-17 326126
91 2023-10-18 366712
92 2023-10-19 336407
[93 rows x 2 columns]
```

### 百度搜索指数
Expand Down
2 changes: 1 addition & 1 deletion docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
**风险提示:[AKShare](https://github.com/akfamily/akshare) 开源财经数据接口库所采集的数据皆来自公开的数据源,不涉及任何个人隐私数据和非公开数据。
同时本项目提供的数据接口及相关数据仅用于学术研究,任何个人、机构及团体使用本项目的数据接口及相关数据请注意商业风险。**

1. 本文档更新时间:**2023-10-19**
1. 本文档更新时间:**2023-10-20**
2. 如有 [AKShare](https://github.com/akfamily/akshare) 库、文档及数据的相关问题,请在 [AKShare Issues](https://github.com/akfamily/akshare/issues) 中提 Issues;
3. 欢迎关注 **数据科学实战** 微信公众号:<div><img src="https://jfds-1252952517.cos.ap-chengdu.myqcloud.com/akshare/readme/qrcode/ds.png"></div>;
4. 如果您的问题未能在文档中找到答案,您也可以加入 **AKShare-VIP QQ 群**: 为了提高问答质量,此群为收费群(一杯咖啡钱即可入群,赠送[《AKShare-初阶-使用教学》](https://zmj.xet.tech/s/wck86)视频课),可以添加 **AKShare-小助手** QQ:1254836886,由小助手邀请入群! ![](https://jfds-1252952517.cos.ap-chengdu.myqcloud.com/akshare/readme/qrcode/qr_code_1254836886.jpg)
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
# 经济政策不确定性(EPU)指数
"article_epu_index" # 主要国家和地区的经济政策不确定性(EPU)指数
# 微博指数
"weibo_index" # 获取3个月内的微博指数
"index_weibo_sina" # 获取3个月内的微博指数
# 百度指数
"baidu_search_index" # 获取百度搜索指数
"baidu_info_index" # 获取百度资讯指数
Expand Down

0 comments on commit e2f6727

Please sign in to comment.