python爬蟲基礎 儲存資料

2021-08-21 06:56:45 字數 1454 閱讀 9005

三、儲存**資訊

使用open()函式以"w"寫入的方式開啟乙個txt文件,如果檔案不存在則python會建立乙個新的文件。然後寫入資料即可。

file=open("test.txt","w")

file.write("new line")

file.close()

將python儲存為csv文件時,需要使用csv模組,具體**如下:

import csv

csvfile=open("test.csv","w+")

try:

writer=csv.writer(csvfile)

writer.writerow(("number","number*2","number*number"))

for i in range(10):

writer.writerow((i,i*2,i*i))

finally:

csvfile.close()

import pymysql

conn=pymysql.connect(host='11*.**3.2**.***',charset='utf8',user='***',passwd='****',db='mysql')

cur=conn.cursor()

cur.execute("use **

**")

#讀取資訊

cur.execute("select * from cars where id=1")

print(cur.fetchone())

# 寫入資訊

cur.execute("insert into cars (carname,carbrand) values("思域","本田")")

("","自主招生入圍合格名單.xls")

python爬蟲資料儲存JSON儲存

encoding utf 8 import re import requests import json from requests.exceptions import requestexception def get open page url try 請求頭 headers 使用localhos...

筆記 爬蟲基礎 資料儲存 05

python連線mysql資料庫 1.安裝pymysql pip install pymysql import pymysql 1.使用pymysql.connect方法鏈結資料庫 db pymysql.connect host localhost port 3306 user root passw...

Python爬蟲 爬蟲獲取資料儲存到檔案

本篇文章 繼續介紹另外兩種方式來實現python爬蟲獲取資料,並將python獲取的資料儲存到檔案中。說明一下我的 環境是python3.7,本地環境是python2.x的可能需要改部分 用python3.x環境的沒問題。coding utf 8 import urllib.requestimpor...