Python爬虫数据处理
一、首先理解下面几个函数
设置变量 length()函数 char_length() replace() 函数 max() 函数
1.1、设置变量 set @变量名=值
set @address='中国-山东省-聊城市-莘县';
select @address
1.2 、length()函数 char_length()函数区别
select length('a')
,char_length('a')
,length('中')
,char_length('中')
1.3、 replace() 函数 和length()函数组合
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个行业等级
select max(length(com_industry)-length(replace(com_industry,'-',''))) as _max_count
from etl1_socom_data
1.4、设置变量 substring_index()字符串截取函数用法
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
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
2.4、表输入2.4、SQL脚本 初步清洗com_district和com_industry字段
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、表输出
注意事项:
① 涉及爬虫增量操作 不要勾选裁剪表选项
②数据连接问题 选择表输出中表所在的数据库
③字段映射问题 确保数据流中的字段和物理表的字段数量一致 对应一致
三、kettle转换etl2清洗
首先建表增加了4个字段 演示步骤在视频里
字段索引 没有提 索引算法建议用BTREE算法增强查询效率
主要针对etl1 生成的新的com_industry进行字段拆分 清洗
3.1.kettle文件名:trans_etl2_socom_data
3.2.包括控件:表输入>>>表输出
3.3.数据流方向:etl1_socom_data>>>>etl2_socom_data
注意事项:
① 涉及爬虫增量操作 不要勾选裁剪表选项
②数据连接问题 选择表输出中表所在的数据库
③字段映射问题 确保数据流中的字段和物理表的字段数量一致 对应一致
3.4、SQL脚本 对com_industry进行拆分 完成所有字段清洗 注册资金字段时间关系没有进行细致拆解 调整代码即可
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 后面应加上表所在的数据库名称
不推荐数据量大的时候使用
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转换执行完毕以后 表输出总量对比
4.3查看etl清洗质量
确保前两个步骤已经无误,数据处理负责的etl清洗工作自查开始 针对数据源清洗的字段 写脚本检查 socom网站主要是对地区 和行业进行了清洗 对其他字段做了替换多余字段处理 ,因此采取脚本检查,
找到page_url和网站数据进行核查
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此页面数据和etl2_socom_data表最终清洗数据对比
清洗工作完成。
学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入学习交流群
626062078,我们一起学Python!
Python爬虫数据处理的更多相关文章
- python 爬虫数据处理字符串时间转换格式方法
startDate = "2018-10-01"endDate = "2018-10-31" ###字符转化为日期startTime = datetime.da ...
- 教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http://www.xiaohuar.com/,让你体验爬取校花的成就感. Scr ...
- 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...
- python爬虫Scrapy(一)-我爬了boss数据
一.概述 学习python有一段时间了,最近了解了下Python的入门爬虫框架Scrapy,参考了文章Python爬虫框架Scrapy入门.本篇文章属于初学经验记录,比较简单,适合刚学习爬虫的小伙伴. ...
- Python爬虫入门教程 12-100 半次元COS图爬取
半次元COS图爬取-写在前面 今天在浏览网站的时候,忽然一个莫名的链接指引着我跳转到了半次元网站 https://bcy.net/ 打开之后,发现也没有什么有意思的内容,职业的敏感让我瞬间联想到了 c ...
- 初识python爬虫框架Scrapy
Scrapy,按照其官网(https://scrapy.org/)上的解释:一个开源和协作式的框架,用快速.简单.可扩展的方式从网站提取所需的数据. 我们一开始上手爬虫的时候,接触的是urllib.r ...
- python爬虫scrapy项目详解(关注、持续更新)
python爬虫scrapy项目(一) 爬取目标:腾讯招聘网站(起始url:https://hr.tencent.com/position.php?keywords=&tid=0&st ...
- Python 爬虫的工具列表大全
Python 爬虫的工具列表大全 这个列表包含与网页抓取和数据处理的Python库.网络 通用 urllib -网络库(stdlib). requests -网络库. grab – 网络库(基于pyc ...
- python爬虫的页面数据解析和提取/xpath/bs4/jsonpath/正则(1)
一.数据类型及解析方式 一般来讲对我们而言,需要抓取的是某个网站或者某个应用的内容,提取有用的价值.内容一般分为两部分,非结构化的数据 和 结构化的数据. 非结构化数据:先有数据,再有结构, 结构化数 ...
随机推荐
- BZOJ3926:[ZJOI2015]诸神眷顾的幻想乡——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3926 https://www.luogu.org/problemnew/show/P3346 幽香 ...
- [bzoj] 1030 文本生成器 || AC自动机+dp
原题 给出n个字符串,求随机生成一个m长度的字符串,有多少个是可辨识的(即出现了n个字符串中的任意字符串) 正难则反 求有多少个不可辨识的,26^m-不可辨识即为答案 f[i][j]表示填到第i个字符 ...
- 学习操作Mac OS 之 常用命令
~ 符号在 Mac 甚至所有基于 Unix 和 Linux 的系统中都是代表当前用户的用户目录,.代表当前目录 配置环境变量语句: source ~/.bash_profile 查看host文件语句 ...
- float,absolute脱离文档流的总结
dom元素脱离文档流,有如下几种方式: 1. float 脱离文档流,其他dom元素无视他,在其下方布局,但是其未脱离文本流,其他元素的文本会认为他存在,环绕他布局.父元素会无视他,因此无法获取其高度 ...
- bzoj 3673&3674 可持久化并查集&加强版(可持久化线段树+启发式合并)
CCZ在2015年8月25日也就是初三暑假要结束的时候就已经能切这种题了%%% 学习了另一种启发式合并的方法,按秩合并,也就是按树的深度合并,实际上是和按树的大小一个道理,但是感觉(至少在这题上)更好 ...
- 【Codeforces 506E】Mr.Kitayuta’s Gift&&【BZOJ 4214】黄昏下的礼物 dp转有限状态自动机+矩阵乘法优化
神题……胡乱讲述一下思维过程……首先,读懂题.然后,转化问题为构造一个长度为|T|+n的字符串,使其内含有T这个子序列.之后,想到一个简单的dp.由于是回文串,我们就增量构造半个回文串,设f(i,j, ...
- purfer序列题表
purfer序列是对于带编号(互不相同)的无根树进行编码得到的,对于同样的n个顶点,其有n-2项,有n^(n-2)种,而且每种都合法(如果只要求他是一棵树的话)(可以通过证明翻译过程维持了各部分的树的 ...
- mesos安装、配置和运行
环境:rhel6.6 安装: 安装依赖包 yum install -y tar wget git which nss yum groupinstall -y "Development Too ...
- POJ 3421分解质因数
X-factor Chains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7375 Accepted: 2340 D ...
- STM32 - 软件设置单片机重启
__set_FAULTMASK();//关闭总中断 NVIC_SystemReset();//请求单片机重启 执行NVIC_SystemReset()函数不允许被打断,所以关总中断