基于python爬蟲(chóng)數(shù)據(jù)處理(詳解)
一、首先理解下面幾個(gè)函數(shù)
設(shè)置變量 length()函數(shù) char_length() replace() 函數(shù) max() 函數(shù)
1.1、設(shè)置變量 set @變量名=值
set @address='中國(guó)-山東省-聊城市-莘縣'; select @address
1.2 、length()函數(shù) char_length()函數(shù)區(qū)別
select length('a')
,char_length('a')
,length('中')
,char_length('中')
1.3、 replace() 函數(shù) 和length()函數(shù)組合
set @address='中國(guó)-山東省-聊城市-莘縣'; select @address ,replace(@address,'-','') as address_1 ,length(@address) as len_add1 ,length(replace(@address,'-','')) as len_add2 ,length(@address)-length(replace(@address,'-','')) as _count
etl清洗字段時(shí)候有明顯分割符的如何確定新的數(shù)據(jù)表增加幾個(gè)分割出的字段
計(jì)算出com_industry中最多有幾個(gè) - 符 以便確定增加幾個(gè)字段 最大值+1 為可以拆分成的字段數(shù) 此表為3 因此可以拆分出4個(gè)行業(yè)字段 也就是4個(gè)行業(yè)等級(jí)
select max(length(com_industry)-length(replace(com_industry,'-',''))) as _max_count from etl1_socom_data
1.4、設(shè)置變量 substring_index()字符串截取函數(shù)用法
set @address='中國(guó)-山東省-聊城市-莘縣'; select substring_index(@address,'-',1) as china, substring_index(substring_index(@address,'-',2),'-',-1) as province, substring_index(substring_index(@address,'-',3),'-',-1) as city, substring_index(@address,'-',-1) as district
1.5、條件判斷函數(shù) case when
case when then when then else 值 end as 字段名 select case when 89>101 then '大于' else '小于' end as betl1_socom_data
二、kettle轉(zhuǎn)換etl1清洗
首先建表 步驟在視頻里
字段索引 沒(méi)有提 索引算法建議用BTREE算法增強(qiáng)查詢效率
2.1.kettle文件名:trans_etl1_socom_data
2.2.包括控件:表輸入>>>表輸出
2.3.數(shù)據(jù)流方向:s_socom_data>>>>etl1_socom_data

kettle轉(zhuǎn)換1截圖
2.4、表輸入2.4、SQL腳本 初步清洗com_district和com_industry字段
select a.*, case when com_district like '%業(yè)' or com_district like '%織' or com_district like '%育' then null else com_district end as com_district1 ,case when com_district like '%業(yè)' or com_district like '%織' or com_district like '%育' then concat(com_district,'-',com_industry) else com_industry end as com_industry_total ,replace(com_addr,'地 址:','') as com_addr1 ,replace(com_phone,'電 話:','') as com_phone1 ,replace(com_fax,'傳 真:','') as com_fax1 ,replace(com_mobile,'手機(jī):','') as com_mobile1 ,replace(com_url,'網(wǎng)址:','') as com_url1 ,replace(com_email,'郵箱:','') as com_email1 ,replace(com_contactor,'聯(lián)系人:','') as com_contactor1 ,replace(com_emploies_nums,'公司人數(shù):','') as com_emploies_nums1 ,replace(com_reg_capital,'注冊(cè)資金:萬(wàn)','') as com_reg_capital1 ,replace(com_type,'經(jīng)濟(jì)類型:','') as com_type1 ,replace(com_product,'公司產(chǎn)品:','') as com_product1 ,replace(com_desc,'公司簡(jiǎn)介:','') as com_desc1 from s_socom_data as a
2.5、表輸出

表輸出設(shè)置注意事項(xiàng)
注意事項(xiàng):
① 涉及爬蟲(chóng)增量操作 不要勾選裁剪表選項(xiàng)
②數(shù)據(jù)連接問(wèn)題 選擇表輸出中表所在的數(shù)據(jù)庫(kù)
③字段映射問(wèn)題 確保數(shù)據(jù)流中的字段和物理表的字段數(shù)量一致 對(duì)應(yīng)一致
三、kettle轉(zhuǎn)換etl2清洗
首先建表增加了4個(gè)字段 演示步驟在視頻里
字段索引 沒(méi)有提 索引算法建議用BTREE算法增強(qiáng)查詢效率
主要針對(duì)etl1 生成的新的com_industry進(jìn)行字段拆分 清洗
3.1.kettle文件名:trans_etl2_socom_data
3.2.包括控件:表輸入>>>表輸出
3.3.數(shù)據(jù)流方向:etl1_socom_data>>>>etl2_socom_data
注意事項(xiàng):
① 涉及爬蟲(chóng)增量操作 不要勾選裁剪表選項(xiàng)
②數(shù)據(jù)連接問(wèn)題 選擇表輸出中表所在的數(shù)據(jù)庫(kù)
③字段映射問(wèn)題 確保數(shù)據(jù)流中的字段和物理表的字段數(shù)量一致 對(duì)應(yīng)一致

kettle轉(zhuǎn)換2截圖
3.4、SQL腳本 對(duì)com_industry進(jìn)行拆分 完成所有字段清洗 注冊(cè)資金字段時(shí)間關(guān)系沒(méi)有進(jìn)行細(xì)致拆解 調(diào)整代碼即可
select a.*, case #行業(yè)為''的值 置為空 when length(com_industry)=0 then null #其他的取第一個(gè)-分隔符之前 else substring_index(com_industry,'-',1) end as com_industry1, case when length(com_industry)-length(replace(com_industry,'-',''))=0 then null #'交通運(yùn)輸、倉(cāng)儲(chǔ)和郵政業(yè)-' 這種值 行業(yè)2 也置為null when length(com_industry)-length(replace(com_industry,'-',''))=1 and length(substring_index(com_industry,'-',-1))=0 then null when length(com_industry)-length(replace(com_industry,'-',''))=1 then substring_index(com_industry,'-',-1) else substring_index(substring_index(com_industry,'-',2),'-',-1) end as com_industry2, case when length(com_industry)-length(replace(com_industry,'-',''))<=1 then null when length(com_industry)-length(replace(com_industry,'-',''))=2 then substring_index(com_industry,'-',-1) else substring_index(substring_index(com_industry,'-',3),'-',-1) end as com_industry3, case when length(com_industry)-length(replace(com_industry,'-',''))<=2 then null else substring_index(com_industry,'-',-1) end as com_industry4 from etl1_socom_data as a
四、清洗效果質(zhì)量檢查
4.1爬蟲(chóng)數(shù)據(jù)源數(shù)據(jù)和網(wǎng)站數(shù)據(jù)是否相符
如果本身工作是爬蟲(chóng)和數(shù)據(jù)處理在一起處理,抓取的時(shí)候其實(shí)已經(jīng)判斷,此步驟可以省略,如果對(duì)接上游爬蟲(chóng)同事,這一步首先判斷,不然清洗也是無(wú)用功,一般都要求爬蟲(chóng)同事存儲(chǔ)請(qǐng)求的url便于后面數(shù)據(jù)處理查看數(shù)據(jù)質(zhì)量
4.2計(jì)算爬蟲(chóng)數(shù)據(jù)源和各etl清洗數(shù)據(jù)表數(shù)據(jù)量
注:SQL腳本中沒(méi)有經(jīng)過(guò)聚合過(guò)濾 3個(gè)表數(shù)據(jù)量應(yīng)相等
4.2.1、sql查詢 下面表我是在同一數(shù)據(jù)庫(kù)中 如果不在同一數(shù)據(jù)庫(kù) from 后面應(yīng)加上表所在的數(shù)據(jù)庫(kù)名稱
不推薦數(shù)據(jù)量大的時(shí)候使用
select count(1) from s_socom_data union all select count(1) from etl1_socom_data union all select count(1) from etl2_socom_data
4.2.2 根據(jù) kettle轉(zhuǎn)換執(zhí)行完畢以后 表輸出總量對(duì)比

kettle表輸出總數(shù)據(jù)量
4.3查看etl清洗質(zhì)量
確保前兩個(gè)步驟已經(jīng)無(wú)誤,數(shù)據(jù)處理負(fù)責(zé)的etl清洗工作自查開(kāi)始 針對(duì)數(shù)據(jù)源清洗的字段 寫(xiě)腳本檢查 socom網(wǎng)站主要是對(duì)地區(qū) 和行業(yè)進(jìn)行了清洗 對(duì)其他字段做了替換多余字段處理 ,因此采取腳本檢查,
找到page_url和網(wǎng)站數(shù)據(jù)進(jìn)行核查
where里面這樣寫(xiě)便于查看某個(gè)字段的清洗情況
select * from etl2_socom_data where com_district is null and length(com_industry)-length(replace(com_industry,'-',''))=3
http://www.socom.cn/company/7320798.html此頁(yè)面數(shù)據(jù)和etl2_socom_data表最終清洗數(shù)據(jù)對(duì)比

網(wǎng)站頁(yè)面數(shù)據(jù)

etl2_socom_data表數(shù)據(jù)
清洗工作完成。
以上這篇基于python爬蟲(chóng)數(shù)據(jù)處理(詳解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python讀寫(xiě)Excel文件庫(kù)的實(shí)現(xiàn)示例
本文主要介紹了Python讀寫(xiě)Excel文件庫(kù)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
python定時(shí)復(fù)制遠(yuǎn)程文件夾中所有文件
這篇文章主要為大家詳細(xì)介紹了python定時(shí)復(fù)制遠(yuǎn)程文件夾中所有文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
Python實(shí)現(xiàn)的徑向基(RBF)神經(jīng)網(wǎng)絡(luò)示例
這篇文章主要介紹了Python實(shí)現(xiàn)的徑向基(RBF)神經(jīng)網(wǎng)絡(luò),結(jié)合完整實(shí)例形式分析了Python徑向基(RBF)神經(jīng)網(wǎng)絡(luò)定義與實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-02-02
對(duì)Python w和w+權(quán)限的區(qū)別詳解
今天小編就為大家分享一篇對(duì)Python w和w+權(quán)限的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Django Admin中增加導(dǎo)出Excel功能過(guò)程解析
這篇文章主要介紹了Django Admin中增加導(dǎo)出Excel功能過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
解決Python 中JSONDecodeError: Expecting value:&n
這篇文章主要介紹了解決Python 中JSONDecodeError: Expecting value: line 1 column 1 (char 0)錯(cuò)誤問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
利用?Python?開(kāi)發(fā)一個(gè)?Python?解釋器
這篇文章主要介紹了利用?Python?開(kāi)發(fā)一個(gè)?Python?解釋器,在本文中,我們將設(shè)計(jì)一個(gè)可以執(zhí)行算術(shù)運(yùn)算的解釋器。下面我們大家一起來(lái)看看吧</P><P>2022-01-01

