檔案寫入方法總結
阿新 • • 發佈:2019-01-04
以下總結爬蟲中常用的檔案寫入方法。
(1)
with open('test.csv','w',encoding='utf-8',newline='') as csvfile:
#設定列名
fieldname = ['website','name']
write = csv.DictWriter(csvfile,fieldnames=fieldname)
#先將頭寫入表中
write.writeheader()
for result in results:
# result是一個字典型別,依次取出我們的每一個值
website = result['website']
name = result['name']
#將我們的值一行一行的寫入到表中
write.writerow({'website':website,'name':name})
#列印我們的結果,注意和上面的寫入進行區分
print('website', website, 'name',name)
(2)
try:
url = response.xpath('//[@id="form"]/m[2]/@href').extract_first( )
except:
url = False
try:
world = response.xpath('//[@id="form"]/m[2]/text()').extract_first()
except:
world = False
try:
chinese = re.findall("排名:<em>(\d+)</em>", response.text)[0]
except:
chinese = False
#將以上三項分別寫入檔案中
row = []
row.append(url)
row.append(world)
row.append(chinese)
#newline='',寫入的時候不生成新的行
out = open('result.csv', 'a', newline='')
# 設定寫入模式,dialect='excel'寫入excel;dialect='csv',寫入csv
csv_write = csv.writer(out, dialect='excel')
# 寫入具體內容
csv_write.writerow(row)
(3)
#將獲取到的name和ID儲存在檔案中
fl = open('nameId.txt', 'w', encoding='utf-8')
fl.write('"'+'name'+'"')
fl.write(',')
fl.write('"'+'id'+'"')
fl.write('\n')
name = 'test'
id ='0001'
fl.write(name+","+id+'\n')