设计一个爬虫,获取小米集团的实时交易价格获取当天开盘价、最高价、最低价、当前价、成交量等信息
时间: 2024-02-02 07:02:30 浏览: 23
这里提供一个Python的爬虫示例,使用BeautifulSoup库进行HTML解析,requests库进行网络请求:
```python
import requests
from bs4 import BeautifulSoup
# 请求小米集团股票页面
url = 'https://finance.yahoo.com/quote/01810.HK'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 解析页面获取所需信息
price_section = soup.find('div', {'class': 'My(6px) Pos(r) smartphone_Mt(6px)'})
price = price_section.find('span').text
change_section = price_section.find('span', {'data-reactid': '33'})
change = change_section.find_all('span')[1].text
change_percent = change_section.find_all('span')[3].text
summary_info = soup.find('div', {'class': 'D(ib) Va(m) Maw(65%) Ov(h)'})
open_price = summary_info.find('td', {'data-reactid': '51'}).text
day_range = summary_info.find('td', {'data-reactid': '52'}).text
volume = summary_info.find('td', {'data-reactid': '56'}).text
# 输出结果
print('Price:', price)
print('Change:', change)
print('Change Percent:', change_percent)
print('Open Price:', open_price)
print('Day Range:', day_range)
print('Volume:', volume)
```
上述代码通过请求小米集团股票页面,使用BeautifulSoup库对HTML进行解析,获取股票价格、价格变化、价格变化百分比、开盘价、最高价、最低价、成交量等信息,并进行输出。