程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python crawler - list information written to CSV file

編輯:Python

List of articles

  • Preface
  • Use steps
    • 1. Import and stock in
    • 2. Read data and write
  • summary


Preface

️ 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 ️


Use steps

1. Import and stock in

The code is as follows ( Example ):

import csv

2. Read data and write

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 ']])

summary

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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved