爬虫数据保存为CSV
时间: 2023-12-17 08:27:19 浏览: 169
爬虫技术-持久化存储之csv文件.pptx
将爬虫数据保存为CSV文件的步骤如下:
1.导入csv模块
```python
import csv
```
2.打开CSV文件并创建csv.writer对象
```python
with open('data.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
```
3.写入表头
```python
writer.writerow(['姓名', '年龄', '性别'])
```
4.写入数据
```python
writer.writerow(['张三', '18', '男'])
writer.writerow(['李四', '20', '女'])
```
5.保存并关闭文件
```python
csvfile.close()
```
如果要将字典类型的数据保存为CSV文件,可以使用csv.DictWriter对象。具体步骤如下:
1.导入csv模块
```python
import csv
```
2.定义表头和数据
```python
fieldnames = ['姓名', '年龄', '性别']
data = [{'姓名': '张三', '年龄': '18', '性别': '男'},
{'姓名': '李四', '年龄': '20', '性别': '女'}]
```
3.打开CSV文件并创建csv.DictWriter对象
```python
with open('data.csv', 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
```
4.写入表头
```python
writer.writeheader()
```
5.写入数据
```python
for row in data:
writer.writerow(row)
```
6.保存并关闭文件
```python
csvfile.close()
```
阅读全文