一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 基于python爬蟲數據處理(詳解)

基于python爬蟲數據處理(詳解)

2020-11-16 19:30腳本之家 Python

下面小編就為大家帶來一篇基于python爬蟲數據處理(詳解)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一、首先理解下面幾個函數

設置變量 length()函數 char_length() replace() 函數 max() 函數

1.1、設置變量 set @變量名=值

?
1
2
set @address='中國-山東省-聊城市-莘縣';
select @address

1.2 、length()函數 char_length()函數區別

?
1
2
3
4
select length('a')
,char_length('a')
,length('中')
,char_length('中')

1.3、 replace() 函數 和length()函數組合

?
1
2
3
4
5
6
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清洗字段時候有明顯分割符的如何確定新的數據表增加幾個分割出的字段

計算出com_industry中最多有幾個 - 符 以便確定增加幾個字段 最大值+1 為可以拆分成的字段數 此表為3 因此可以拆分出4個行業字段 也就是4個行業等級

?
1
2
select max(length(com_industry)-length(replace(com_industry,'-',''))) as _max_count
from etl1_socom_data

1.4、設置變量 substring_index()字符串截取函數用法

?
1
2
3
4
5
6
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、條件判斷函數 case when

?
1
2
3
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.數據流方向:s_socom_data>>>>etl1_socom_data

基于python爬蟲數據處理(詳解)

kettle轉換1截圖

2.4、表輸入2.4、SQL腳本 初步清洗com_district和com_industry字段

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
select a.*,
case when com_district like '%業' or com_district like '%織' or com_district like '%育' then null else com_district end as com_district1
,case when com_district like '%業' 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,'網址:','') as com_url1
,replace(com_email,'郵箱:','') as com_email1
,replace(com_contactor,'聯系人:','') as com_contactor1
,replace(com_emploies_nums,'公司人數:','') as com_emploies_nums1
,replace(com_reg_capital,'注冊資金:萬','') as com_reg_capital1
,replace(com_type,'經濟類型:','') as com_type1
,replace(com_product,'公司產品:','') as com_product1
,replace(com_desc,'公司簡介:','') as com_desc1
from s_socom_data as a

2.5、表輸出

基于python爬蟲數據處理(詳解)

表輸出設置注意事項

注意事項:

① 涉及爬蟲增量操作 不要勾選裁剪表選項

②數據連接問題 選擇表輸出中表所在的數據庫

③字段映射問題 確保數據流中的字段和物理表的字段數量一致 對應一致

三、kettle轉換etl2清洗

首先建表增加了4個字段 演示步驟在視頻里

字段索引 沒有提 索引算法建議用BTREE算法增強查詢效率

主要針對etl1 生成的新的com_industry進行字段拆分 清洗

3.1.kettle文件名:trans_etl2_socom_data

3.2.包括控件:表輸入>>>表輸出

3.3.數據流方向:etl1_socom_data>>>>etl2_socom_data

注意事項:

① 涉及爬蟲增量操作 不要勾選裁剪表選項

②數據連接問題 選擇表輸出中表所在的數據庫

③字段映射問題 確保數據流中的字段和物理表的字段數量一致 對應一致

基于python爬蟲數據處理(詳解)

kettle轉換2截圖

3.4、SQL腳本 對com_industry進行拆分 完成所有字段清洗 注冊資金字段時間關系沒有進行細致拆解 調整代碼即可

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
select a.*,
case
#行業為''的值 置為空
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
#'交通運輸、倉儲和郵政業-' 這種值 行業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爬蟲數據源數據和網站數據是否相符

如果本身工作是爬蟲和數據處理在一起處理,抓取的時候其實已經判斷,此步驟可以省略,如果對接上游爬蟲同事,這一步首先判斷,不然清洗也是無用功,一般都要求爬蟲同事存儲請求的url便于后面數據處理查看數據質量

4.2計算爬蟲數據源和各etl清洗數據表數據量

注:SQL腳本中沒有經過聚合過濾 3個表數據量應相等

4.2.1、sql查詢 下面表我是在同一數據庫中 如果不在同一數據庫 from 后面應加上表所在的數據庫名稱

不推薦數據量大的時候使用

?
1
2
3
4
5
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 根據 kettle轉換執行完畢以后 表輸出總量對比

基于python爬蟲數據處理(詳解)

kettle表輸出總數據量

4.3查看etl清洗質量

確保前兩個步驟已經無誤,數據處理負責的etl清洗工作自查開始 針對數據源清洗的字段 寫腳本檢查 socom網站主要是對地區 和行業進行了清洗 對其他字段做了替換多余字段處理 ,因此采取腳本檢查,

找到page_url和網站數據進行核查

where里面這樣寫便于查看某個字段的清洗情況

?
1
2
3
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此頁面數據和etl2_socom_data表最終清洗數據對比

基于python爬蟲數據處理(詳解)

網站頁面數據

基于python爬蟲數據處理(詳解)

etl2_socom_data表數據

清洗工作完成。

以上這篇基于python爬蟲數據處理(詳解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产成人精品免费视频大全五级 | 国产精品青青青高清在线观看 | 97国产影院 | 波多野结衣两女调教 | 国色天香论坛社区在线视频 | 小小水蜜桃免费影院 | eee在线播放成人免费 | 日本免费久久久久久久网站 | 国产ab | 日韩欧美国产一区 | 无人在线视频高清免费观看动漫 | 美女国内精品自产拍在线播放 | 国产在线视频一区二区三区 | 描写细腻的高h肉 | 国内精品久久久久影院男同志 | 国产最新精品视频 | 亚洲精品一区二区三区在线播放 | 国产高清不卡码一区二区三区 | 奇米影视999| 国产精品亚洲专区在线播放 | 亚洲国产99在线精品一区二区 | 久草热8精品视频在线观看 久草草在线视视频 | 韩国理论片最新第一页 | 久久久久久免费高清电影 | 亚洲国产精品日韩高清秒播 | 亚洲精品成人AV在线观看爽翻 | 日本动漫打扑克动画片樱花动漫 | 色中文网 | 视频一区二区国产无限在线观看 | 国产精品第一 | 日本精品一卡二卡≡卡四卡 | 韩国一大片a毛片女同 | 免费观看俄罗斯特黄特色 | 国产欧美一区二区三区免费看 | 男女啪啪gif | 性一交一无一伦一精一品 | 海绵宝宝第二季全集免费观看 | 国产一区二区视频免费 | 极品蜜桃臀美女啪啪 | a级成人毛片免费图片 | 美女的隐私视频免费看软件 |