本文作者:刘恒新,河南大学经济学院
本文编辑:马梦杰
技术总编:李婷婷
Stata and Python 数据分析
爬虫俱乐部Stata基础课程、Stata进阶课程和Python课程可在小鹅通平台查看,欢迎大家多多支持订阅!如需了解详情,可以通过课程链接(https://appbqiqpzi66527.h5.xiaoeknow.com/homepage/10)或课程二维码进行访问哦~
1.导读
2.数据获取的准备工作
首先确定本文的数据来源,我们首先找到宗祠网官网:“www.100citang.cn/zongciweb/citang” 找到如下图所示的页面:我们想要获取的数据是:宗祠名称、宗祠所在地、所处省份、所处城市一共四部分信息。

3.获取源代码

*单个网页抓取clearcap mkdir "D:\stata"cd "D:\stata"copy "http://www.100citang.cn/zongciweb/citang" temp.txt, replaceinfix strL v 1-100000 using temp.txt, clear

4.数据处理
在对源代码进行观察后,我们使用以下代码对数据进行初步处理,保留所需信息:
*对数据进行初步处理keep if index(v, "class=") //保留含有"</div></td>"标签的行drop if index(v, "</li>")drop if index(v, "<div")drop in 1/10drop in -12/-1*删去无用符号replace v = subinstr(v, "</p>", "", .)replace v = subinstr(v, "</h5>", "", .)replace v = subinstr(v, `"<h5 class="name">"', "", .)replace v = subinstr(v, `"<p class="address">"', "", .)*分组得到想要的数据replace v = v + "*****" + v[_n + 1] + "*****" + v[_n + 2] + "*****" + v[_n + 3] if mod(_n, 4) == 1split v, p("*****")keep if mod(_n, 4) == 1drop vdrop v1drop v4rename v2 宗祠名称rename v3 地点

5.划分省市区
*省份数据获取gen 省份=ustrregexs(0) if ustrregexm(地点, ".*省+") //提取省份replace 省份=ustrregexs(0) if ustrregexm(地点, ".*?自治区+") //自治区replace 省份=ustrregexs(0) if ustrregexm(地点,".*?市+") & 省份=="" //直辖市replace 省份=地点 if 省份==""*城市数据获取gen city=ustrregexs(0) if ustrregexm(地点, "省.*?自治州+") //提取省辖自治州replace city=ustrregexra(city,"[省]","")gen city1=ustrregexs(0) if ustrregexm(地点, "省.*?市+") //省辖地市replace city1=ustrregexra(city1,"[省]","")replace city=city1 if city==""drop city1gen city1=ustrregexs(0) if ustrregexm(地点, "自治区.*?地区+") //提取自治区辖地区replace city1=usubstr(city1, 4, strlen(city1)) //删掉"自治区"replace city=city1 if city==""drop city1gen city1=ustrregexs(0) if ustrregexm(地点, "自治区.*?自治州+") //自治区辖自治州replace city1=usubstr(city1, 4, strlen(city1)) //删掉"自治区"replace city=city1 if city==""drop city1gen city1=ustrregexs(0) if ustrregexm(地点, "自治区.*?市+") //自治区辖地市replace city1=usubstr(city1, 4, strlen(city1)) //删掉"自治区"replace city=city1 if city==""drop city1gen city1=ustrregexs(0) if ustrregexm(地点, ".*?市+") //再提取直辖市replace city=city1 if city==""drop city1*县区数据获取gen cnty=ustrregexs(0) if ustrregexm(地点, "\市.*") //保留县及以下地址gen cnty1=ustrregexs(0) if ustrregexm(地点, "\自治州.*")gen cnty2=ustrregexs(0) if ustrregexm(地点, "\地区.*")gen cnty3=usubstr(cnty, 2, strlen(cnty))replace cnty3=usubstr(cnty1, 4, strlen(cnty1)) if cnty3==""replace cnty3=usubstr(cnty2, 3, strlen(cnty2)) if cnty3==""drop cnty cnty1 cnty2gen cnty=ustrregexs(0) if ustrregexm(cnty3, ".*?县+") //提取县replace cnty=ustrregexs(0) if ustrregexm(cnty3, ".*?市+") & cnty=="" //提取县级市replace cnty=ustrregexs(0) if ustrregexm(cnty3, ".*?区+") & cnty=="" //提取市辖区replace cnty=city if cnty=="" //将city中的县级市替换到cntydrop cnty3gen cnty1=ustrregexs(0) if ustrregexm(地点,"省.*?县+") //无市地址,只有省县replace cnty1=usubstr(cnty1, 2, strlen(cnty1)) //删掉"省"replace cnty=cnty1 if cnty==""drop cnty1

6.汇总各地区宗祠数量
*统计各城市宗祠数量bys city: gen n=_ndrop in 1/17bys city: egen 各市宗祠数量=max(n)drop n*统计各省份宗祠数量bys prov: gen n=_nbys prov: egen 各省宗祠数量=max(n)drop nsave 宗祠.dta,replace

重磅福利!为了更好地服务各位同学的研究,爬虫俱乐部将在小鹅通平台上持续提供金融研究所需要的各类指标,包括上市公司十大股东、股价崩盘、投资效率、融资约束、企业避税、分析师跟踪、净资产收益率、资产回报率、国际四大审计、托宾Q值、第一大股东持股比例、账面市值比、沪深A股上市公司研究常用控制变量等一系列深加工数据,基于各交易所信息披露的数据利用Stata在实现数据实时更新的同时还将不断上线更多的数据指标。我们以最前沿的数据处理技术、最好的服务质量、最大的诚意望能助力大家的研究工作!相关数据链接,请大家访问:(https://appbqiqpzi66527.h5.xiaoeknow.com/homepage/10)或扫描二维码:
对我们的推文累计打赏超过1000元,我们即可给您开具发票,发票类别为“咨询费”。用心做事,不负您的支持!
往期推文推荐
【Python实战】基于大数据的贷款违约分析
【数据分析基础】Python数据交叉表
Stata压缩和解压缩文件的几种方法
【python库——whisper】实现音频转换成文字功能
用Python进行乳腺癌预测的简单机器学习
足不出户的“世界之旅”
【爬虫实战】python文本分析库——Gensim
Python可视化-绘制三维空间空间图形
微信公众号“Stata and Python数据分析”分享实用的Stata、Python等软件的数据处理知识,欢迎转载、打赏。我们是由李春涛教授领导下的研究生及本科生组成的大数据处理和分析团队。
武汉字符串数据科技有限公司一直为广大用户提供数据采集和分析的服务工作,如果您有这方面的需求,请发邮件到statatraining@163.com,或者直接联系我们的数据中台总工程司海涛先生,电话:18203668525,wechat: super4ht。海涛先生曾长期在香港大学从事研究工作,现为知名985大学的博士生,爬虫俱乐部网络爬虫技术和正则表达式的课程负责人。
此外,欢迎大家踊跃投稿,介绍一些关于Stata和Python的数据处理和分析技巧。

