使用 CSV 文件存储
将爬取到的数据以 CSV 文件形式存储:
import csv import requests req = requests.get("http://www.baidu.com/") data = { 'status_code': req.status_code, 'url': req.url, 'cookies': req.cookies, } # 写入到CSV文件 with open('/tmp/1.csv', 'w') as csvfile: fieldnames = ['status_code', 'url', 'cookies'] write = csv.DictWriter(csvfile, fieldnames=fieldnames) write.writerow(data) # 从CSV文件中读取数据 with open('/tmp/1.csv', 'r') as csvfile: reader = csv.reader(csvfile) for row in reader: print(row)