使用Python爬蟲怎么導(dǎo)出CSV文件,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)公司專注于荷塘企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),成都商城網(wǎng)站開發(fā)。荷塘網(wǎng)站建設(shè)公司,為荷塘等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站設(shè)計,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)核心代碼:
####寫入Csv文件中 with open(self.CsvFileName, 'wb') as csvfile: spamwriter = csv.writer(csvfile, dialect='excel') #設(shè)置標(biāo)題 spamwriter.writerow(["游戲賬號","用戶類型","游戲名稱","渠道","充值類型","充值金額","返利金額","單號","日期"]) #將CsvData中的數(shù)據(jù)循環(huán)寫入到CsvFileName文件中 for item in self.CsvData: spamwriter.writerow(item)
完整代碼:
# coding=utf-8
import urllib
import urllib2
import cookielib
import re
import csv
import sys
class Pyw():
#初始化數(shù)據(jù)
def __init__(self):
#登錄的Url地址
self.LoginUrl="http://v.pyw.cn/login/check"
#所要獲取的Url地址
self.PageUrl="http://v.pyw.cn/Data/accountdetail/%s"
# 傳輸?shù)臄?shù)據(jù):用戶名、密碼、是否記住用戶名
self.PostData = urllib.urlencode({
"username": "15880xxxxxx",
"password": "a123456",
"remember": "1"
})
#第幾筆記錄
self.PageIndex=0;
#循環(huán)獲取共4頁內(nèi)容
self.PageTotal=1
#正則解析出tr
self.TrExp=re.compile("(?isu)<tr[^>]*>(.*?)</tr>")
#正則解析出td
self.TdExp = re.compile("(?isu)<td[^>]*>(.*?)</td>")
#創(chuàng)建cookie
self.cookie = cookielib.CookieJar()
#構(gòu)建opener
self.opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))
#解析頁面總頁數(shù)
self.Total=4
#####設(shè)置csv文件
self.CsvFileName="Pyw.csv"
#####存儲Csv數(shù)據(jù)
self.CsvData=[]
#解析網(wǎng)頁中的內(nèi)容
def GetPageItem(self,PageHtml):
#循環(huán)取出Table中的所有行
for row in self.TrExp.findall(PageHtml):
#取出當(dāng)前行的所有列
coloumn=self.TdExp.findall(row)
#判斷符合的記錄
if len(coloumn) == 9:
# print "游戲賬號:%s" % coloumn[0].strip()
# print "用戶類型:%s" % coloumn[1].strip()
# print "游戲名稱:%s" % coloumn[2].strip()
# print "渠道:%s" % coloumn[3].strip()
# print "充值類型:%s" % coloumn[4].strip()
# print "充值金額:%s" % coloumn[5].strip().replace("¥", "")
# print "返利金額:%s" % coloumn[6].strip().replace("¥", "")
# print "單號:%s" % coloumn[7].strip()
# print "日期:%s" % coloumn[8].strip()
#拼湊行數(shù)據(jù)
d=[coloumn[0].strip(),
coloumn[1].strip(),
coloumn[2].strip(),
coloumn[3].strip(),
coloumn[4].strip(),
coloumn[5].strip().replace("¥", ""),
coloumn[6].strip().replace("¥", ""),
coloumn[7].strip(),
coloumn[8].strip()]
self.CsvData.append(d)
#模擬登錄并獲取頁面數(shù)據(jù)
def GetPageHtml(self):
try:
#模擬登錄
request=urllib2.Request(url=self.LoginUrl,data=self.PostData)
ResultHtml=self.opener.open(request)
#開始執(zhí)行獲取頁面數(shù)據(jù)
while self.PageTotal<=self.Total:
#動態(tài)拼湊所要解析的Url
m_PageUrl = self.PageUrl % self.PageTotal
#計算當(dāng)期第幾頁
self.PageTotal = self.PageTotal + 1
#獲取當(dāng)前解析頁面的所有內(nèi)容
ResultHtml=self.opener.open(m_PageUrl)
#解析網(wǎng)頁中的內(nèi)容
self.GetPageItem(ResultHtml.read())
####寫入Csv文件中
with open(self.CsvFileName, 'wb') as csvfile:
spamwriter = csv.writer(csvfile, dialect='excel')
#設(shè)置標(biāo)題
spamwriter.writerow(["游戲賬號","用戶類型","游戲名稱","渠道","充值類型","充值金額","返利金額","單號","日期"])
#將CsvData中的數(shù)據(jù)循環(huán)寫入到CsvFileName文件中
for item in self.CsvData:
spamwriter.writerow(item)
print "成功導(dǎo)出CSV文件!"
except Exception,e:
print "404 error!%s" % e
#實例化類
p=Pyw()
#執(zhí)行方法
p.GetPageHtml()導(dǎo)出結(jié)果

關(guān)于使用Python爬蟲怎么導(dǎo)出CSV文件問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道了解更多相關(guān)知識。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文標(biāo)題:使用Python爬蟲怎么導(dǎo)出CSV文件-創(chuàng)新互聯(lián)
文章URL:http://www.chinadenli.net/article26/dhshjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、微信小程序、用戶體驗、全網(wǎng)營銷推廣、服務(wù)器托管、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容