python爬取齐翔腾达股票历史数据
时间: 2023-11-17 09:06:03 浏览: 144
以下是Python爬取齐翔腾达股票历史数据的步骤:
1. 导入必要的库
```python
import requests
from bs4 import BeautifulSoup
import pandas as pd
```
2. 获取网页内容
```python
url = 'http://quotes.money.163.com/trade/lsjysj_002408.html'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
```
3. 解析网页内容
```python
table = soup.find('table', {'class': 'table_bg001 border_box limit_sale'})
thead = table.find('thead')
tr = thead.find('tr')
columns = []
for th in tr.find_all('th'):
columns.append(th.text)
tbody = table.find('tbody')
rows = []
for tr in tbody.find_all('tr'):
row = []
for td in tr.find_all('td'):
row.append(td.text)
rows.append(row)
```
4. 将数据转换为DataFrame格式
```python
df = pd.DataFrame(rows, columns=columns)
```
5. 将数据保存为CSV文件
```python
df.to_csv('002408.csv', index=False)
```
阅读全文