设计一个爬虫,获取股票实时交易价格当天开盘价、最高价、最低价、当前价、成交量
时间: 2023-10-26 21:08:21 浏览: 48
以下是一个Python示例爬虫,使用了requests和beautifulsoup4库来获取股票实时交易价格:
```python
import requests
from bs4 import BeautifulSoup
# 股票代码
stock_code = '600519'
# 构造URL
url = f'https://finance.sina.com.cn/realstock/company/{stock_code}/nc.shtml'
# 发送请求并获取响应
response = requests.get(url)
# 解析HTML
soup = BeautifulSoup(response.text, 'html.parser')
# 获取数据
open_price = soup.select('.table-container .tb01 tr:nth-child(1) td:nth-child(2)')[0].text.strip()
high_price = soup.select('.table-container .tb01 tr:nth-child(2) td:nth-child(2)')[0].text.strip()
low_price = soup.select('.table-container .tb01 tr:nth-child(3) td:nth-child(2)')[0].text.strip()
current_price = soup.select('.table-container .tb01 tr:nth-child(4) td:nth-child(2)')[0].text.strip()
volume = soup.select('.table-container .tb01 tr:nth-child(7) td:nth-child(2)')[0].text.strip()
# 打印结果
print(f'开盘价:{open_price}')
print(f'最高价:{high_price}')
print(f'最低价:{low_price}')
print(f'当前价:{current_price}')
print(f'成交量:{volume}')
```
这个示例爬虫使用了新浪财经的股票页面来获取数据。你可以更改`stock_code`变量的值来查询不同的股票。注意,这个爬虫仅供学习和研究目的使用,请勿用于非法用途。