爬取数据变成Excel表
时间: 2023-09-06 17:13:32 浏览: 43
爬取数据后,可以使用Python中的pandas库将数据保存成Excel表格。
首先需要安装pandas库:
```
pip install pandas
```
接着,将爬取到的数据保存成DataFrame格式,然后使用to_excel方法将数据保存成Excel表格。
例如,爬取到的数据如下:
```
data = [
{'name': 'John', 'age': 20, 'gender': 'male'},
{'name': 'Amy', 'age': 25, 'gender': 'female'},
{'name': 'Bob', 'age': 30, 'gender': 'male'},
{'name': 'Lisa', 'age': 28, 'gender': 'female'}
]
```
将数据保存成DataFrame格式:
```
import pandas as pd
df = pd.DataFrame(data)
```
然后将数据保存成Excel表格,可以使用to_excel方法:
```
df.to_excel('data.xlsx', index=False)
```
其中,index=False表示不保存行索引。
最终生成的Excel表格如下:
| name | age | gender |
| ---- | --- | ------ |
| John | 20 | male |
| Amy | 25 | female |
| Bob | 30 | male |
| Lisa | 28 | female |