生成一段爬取A股信息的python代码
时间: 2024-01-07 15:05:30 浏览: 95
当然可以,以下是一段简单的Python代码,可以获取A股市场的股票列表和实时股票数据。
```python
import requests
import json
# 获取股票列表
def get_stock_list():
url = "http://api.money.126.net/data/hs/klinelist/"
res = requests.get(url)
data = res.json()
stock_list = [item['code'] for item in data['list']]
return stock_list
# 获取实时股票数据
def get_stock_data(stock_code):
url = "http://api.money.126.net/data/hs/time/today/{}.json".format(stock_code)
res = requests.get(url)
data = res.json()
stock_name = data['name']
current_price = data['time'][-1]['price']
change_percent = data['time'][-1]['percent']
return "{} 当前价格:{},涨跌幅:{}".format(stock_name, current_price, change_percent)
if __name__ == '__main__':
stock_list = get_stock_list()
print("A股市场股票列表:")
print(stock_list)
stock_code = "000001" # 上证指数
stock_data = get_stock_data(stock_code)
print(stock_data)
```
希望对你有所帮助。
阅读全文