️ When we were crawling , Whether there are the following requirements ? You need to store the crawled data list in a csv In the document ? Then the steps are as follows ️
The code is as follows ( Example ):
import csv
Below data_list
It is obtained in front of the code
The code is as follows ( Example ):
# Write data
data_list = [
{
' News headlines ': ' Our school successfully completed 2022 In the first half of the year, the national CET-4 and CET-6 oral test ', ' Release time ': '2022-05-24 09:18 ', ' News link ': 'http://www.cqwu.edu.cn/article_331608.html', ' Reading times ': '772', ' News source ': ' dean's office Tangjiarong '},
{
' News headlines ': ' The school held 2022 Annual faculty double buckle competition ', ' Release time ': '2022-05-24 08:40 ', ' News link ': 'http://www.cqwu.edu.cn/article_331587.html', ' Reading times ': '898', ' News source ': ' Wu Bo '},
{
' News headlines ': ' The school language and writing working committee organized young volunteers to popularize Putonghua and help revitalize rural culture ', ' Release time ': '2022-05-23 17:44 ', ' News link ': 'http://www.cqwu.edu.cn/article_331556.html', ' Reading times ': '907', ' News source ': ' dean's office Bluefin '}]
# 1. establish csv File object ,encoding='utf-8' Is to set the encoding format ,newline='' To prevent blank lines
f = open('news.csv', 'w', encoding='utf-8')
# 2. Build on file objects csv Write object
csv_write = csv.writer(f)
# 3. Build list headers
csv_write.writerow([' News headlines ', ' Release time ', ' News link ', ' Reading times ', ' News source '])
for data in data_list:
# 4. write in csv file
csv_write.writerow([data[' News headlines '], data[' Release time '], data[' News link '], data[' Reading times '], data[' News source ']])
give the result as follows
such , Our data will be stored successfully
summary : Need a list list
The data dictionary dict
, utilize csv library
You can put the list directly list
Information output to csv
Format file