基于python爬蟲數(shù)據(jù)處理(詳解)
一、首先理解下面幾個函數(shù)
設置變量 length()函數(shù) char_length() replace() 函數(shù) max() 函數(shù)
1.1、設置變量 set @變量名=值
set @address='中國-山東省-聊城市-莘縣'; 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='中國-山東省-聊城市-莘縣'; 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ù)據(jù)表增加幾個分割出的字段
計算出com_industry中最多有幾個 - 符 以便確定增加幾個字段 最大值+1 為可以拆分成的字段數(shù) 此表為3 因此可以拆分出4個行業(yè)字段 也就是4個行業(yè)等級
select max(length(com_industry)-length(replace(com_industry,'-',''))) as _max_count from etl1_socom_data
1.4、設置變量 substring_index()字符串截取函數(shù)用法
set @address='中國-山東省-聊城市-莘縣'; 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轉換etl1清洗
首先建表 步驟在視頻里
字段索引 沒有提 索引算法建議用BTREE算法增強查詢效率
2.1.kettle文件名:trans_etl1_socom_data
2.2.包括控件:表輸入>>>表輸出
2.3.數(shù)據(jù)流方向:s_socom_data>>>>etl1_socom_data
kettle轉換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,'手機:','') 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,'注冊資金:萬','') as com_reg_capital1 ,replace(com_type,'經(jīng)濟類型:','') as com_type1 ,replace(com_product,'公司產(chǎn)品:','') as com_product1 ,replace(com_desc,'公司簡介:','') as com_desc1 from s_socom_data as a
2.5、表輸出
表輸出設置注意事項
注意事項:
① 涉及爬蟲增量操作 不要勾選裁剪表選項
②數(shù)據(jù)連接問題 選擇表輸出中表所在的數(shù)據(jù)庫
③字段映射問題 確保數(shù)據(jù)流中的字段和物理表的字段數(shù)量一致 對應一致
三、kettle轉換etl2清洗
首先建表增加了4個字段 演示步驟在視頻里
字段索引 沒有提 索引算法建議用BTREE算法增強查詢效率
主要針對etl1 生成的新的com_industry進行字段拆分 清洗
3.1.kettle文件名:trans_etl2_socom_data
3.2.包括控件:表輸入>>>表輸出
3.3.數(shù)據(jù)流方向:etl1_socom_data>>>>etl2_socom_data
注意事項:
① 涉及爬蟲增量操作 不要勾選裁剪表選項
②數(shù)據(jù)連接問題 選擇表輸出中表所在的數(shù)據(jù)庫
③字段映射問題 確保數(shù)據(jù)流中的字段和物理表的字段數(shù)量一致 對應一致
kettle轉換2截圖
3.4、SQL腳本 對com_industry進行拆分 完成所有字段清洗 注冊資金字段時間關系沒有進行細致拆解 調整代碼即可
select a.*, case #行業(yè)為''的值 置為空 when length(com_industry)=0 then null #其他的取第一個-分隔符之前 else substring_index(com_industry,'-',1) end as com_industry1, case when length(com_industry)-length(replace(com_industry,'-',''))=0 then null #'交通運輸、倉儲和郵政業(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
四、清洗效果質量檢查
4.1爬蟲數(shù)據(jù)源數(shù)據(jù)和網(wǎng)站數(shù)據(jù)是否相符
如果本身工作是爬蟲和數(shù)據(jù)處理在一起處理,抓取的時候其實已經(jīng)判斷,此步驟可以省略,如果對接上游爬蟲同事,這一步首先判斷,不然清洗也是無用功,一般都要求爬蟲同事存儲請求的url便于后面數(shù)據(jù)處理查看數(shù)據(jù)質量
4.2計算爬蟲數(shù)據(jù)源和各etl清洗數(shù)據(jù)表數(shù)據(jù)量
注:SQL腳本中沒有經(jīng)過聚合過濾 3個表數(shù)據(jù)量應相等
4.2.1、sql查詢 下面表我是在同一數(shù)據(jù)庫中 如果不在同一數(shù)據(jù)庫 from 后面應加上表所在的數(shù)據(jù)庫名稱
不推薦數(shù)據(jù)量大的時候使用
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轉換執(zhí)行完畢以后 表輸出總量對比
kettle表輸出總數(shù)據(jù)量
4.3查看etl清洗質量
確保前兩個步驟已經(jīng)無誤,數(shù)據(jù)處理負責的etl清洗工作自查開始 針對數(shù)據(jù)源清洗的字段 寫腳本檢查 socom網(wǎng)站主要是對地區(qū) 和行業(yè)進行了清洗 對其他字段做了替換多余字段處理 ,因此采取腳本檢查,
找到page_url和網(wǎng)站數(shù)據(jù)進行核查
where里面這樣寫便于查看某個字段的清洗情況
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此頁面數(shù)據(jù)和etl2_socom_data表最終清洗數(shù)據(jù)對比
網(wǎng)站頁面數(shù)據(jù)
etl2_socom_data表數(shù)據(jù)
清洗工作完成。
以上這篇基于python爬蟲數(shù)據(jù)處理(詳解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python實現(xiàn)隨機生成手機號及正則驗證手機號的方法
這篇文章主要介紹了Python實現(xiàn)隨機生成手機號及正則驗證手機號的方法,涉及Python基于random模塊的隨機數(shù)以及基于re模塊的正則驗證相關操作技巧,需要的朋友可以參考下2018-04-04Python Django簡單實現(xiàn)session登錄注銷過程詳解
這篇文章主要介紹了Python Django簡單實現(xiàn)session登錄注銷過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08Python使用logging實現(xiàn)多進程安全的日志模塊
這篇文章主要為大家詳細介紹了Python如何使用標準庫logging實現(xiàn)多進程安全的日志模塊,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2024-01-01pytorch實現(xiàn)加載保存查看checkpoint文件
這篇文章主要介紹了pytorch實現(xiàn)加載保存查看checkpoint文件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07Python ADF 單位根檢驗 如何查看結果的實現(xiàn)
這篇文章主要介紹了Python ADF 單位根檢驗 如何查看結果的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python辦公自動化之數(shù)據(jù)預處理和數(shù)據(jù)校驗詳解
這篇文章主要為大家詳細介紹了Python辦公自動化中數(shù)據(jù)預處理和數(shù)據(jù)校驗的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下2024-01-01