Python簡單爬蟲導(dǎo)出CSV文件的實例講解
流程:模擬登錄→獲取Html頁面→正則解析所有符合條件的行→逐一將符合條件的行的所有列存入到CSVData[]臨時變量中→寫入到CSV文件中
核心代碼:
####寫入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é)果
以上這篇Python簡單爬蟲導(dǎo)出CSV文件的實例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用python將csv數(shù)據(jù)導(dǎo)入mysql數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了如何使用python將csv數(shù)據(jù)導(dǎo)入mysql數(shù)據(jù)庫,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-05-05python內(nèi)建類型與標(biāo)準(zhǔn)類型
這篇文章主要介紹了python內(nèi)建類型與標(biāo)準(zhǔn)類型,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08Python繪制圣誕樹+落葉+雪花+背景音樂+浪漫彈窗?五合一版圣誕樹
馬上不就到圣誕節(jié)了嘛,我看到朋友圈里很多小伙伴再紛紛炫耀自己收到的專屬圣誕樹,今天小編給大家介紹的是通過Python繪制的五合一版圣誕樹:圣誕樹+落葉+雪花+背景音樂+浪漫彈窗。感興趣的小伙伴快來學(xué)習(xí)一下吧2021-12-12利用pandas進(jìn)行數(shù)據(jù)清洗的7種方式
采集到原始的數(shù)據(jù)中會存在一些噪點數(shù)據(jù),噪點數(shù)據(jù)是對分析無意義或者對分析起到偏執(zhí)作用的數(shù)據(jù),所以這篇文章給大家介紹了利用pandas進(jìn)行數(shù)據(jù)清洗的7種方式,需要的朋友可以參考下2024-03-03Python 類,對象,數(shù)據(jù)分類,函數(shù)參數(shù)傳遞詳解
這篇文章主要介紹了深入理解Python 類,對象,數(shù)據(jù)分類,函數(shù)參數(shù)傳遞,涉及具體代碼示例,具有一定參考價值,需要的朋友可以了解下。2021-09-09