美文网首页大数据 爬虫Python AI SqlPython小哥哥
python网页爬虫实战:PEER数据库地震波批量下载 !

python网页爬虫实战:PEER数据库地震波批量下载 !

作者: 14e61d025165 | 来源:发表于2019-06-28 15:43 被阅读2次

今年6月TIOBE公布了最新编程语言排行榜,python继续保持强劲的上升势头并一举超越老牌编程语言C++,跃升至榜单第三名。python大火的原因得益于简单易懂的语句结构和丰富强大的功能包。随着人工智能、机器学习和大数据科学日益兴起和火爆,python迅速进入大众视野,甚至推动了少儿编程。地震工程同样是一门基于大数据的科学,全球数以万计的学者的研究资料和时刻发生的地震的地面运动数据构成了极其庞大的数据库。因此,python可以帮助从事地震工程研究的学者轻松愉快地获得大量数据。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561707770245 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

2019年6月TIOBE编程语言排行榜

Python学习交流群:1004391443

在地震工程领域,地震波数据的获取可谓是研究的基础,无论是动力时程分析和动力增量分析还是振动台试验都需要大量地震波数据。抗震新西兰今年2月曾推出两期连载介绍如何使用PEER数据库(详见PEER地面运动数据库使用教程Ⅰ:搜索原始数据和PEER地面运动数据库使用教程Ⅱ:创建反应谱),但当需要大量地震波数据时,手动下载就显得费时费力,此时python就能发挥出巨大优势批量下载数据。

准备工作

python是完全免费的编程语言,只需到python官网https://www.python.org/下载即可,目前最新的版本python3.7.3。接着是安装python的编译器,目前主流的编译器有PyCharm和Anaconda,也都是免费下载使用。

网页爬虫需要安装功能包selenium,PyCharm和Anaconda均有海量的功能包库,搜索selenium安装即可。为了实现selenium自动打开Google浏览器,需到http://chromedriver.chromium.org/下载ChromeDriver。

代码解读

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561707770249 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

第一步导入seleniumn功能包。主要使用的函数是webdriver,其功能是自动打开网页。接着输入准备下载的地震波的RSN编号,教程以RSN编号为1,100,200,300和600的五条地震波为例。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561707770254 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

第二步登陆PEER数据库。首先定义变量browser用webdriver打开ChromeDriver,语句为browser = webdriver.Chrome(' '),括号里输入ChromeDriver的位置。接着打开PEER数据库网页,语句为browser.get('https://ngawest2.berkeley.edu/'),点击进入NGA-West2数据库,采用了find_element_by_xpath函数,获取enter的xpath输入括号内即可模拟点击enter。为了防止seleniumn找不到新界面的值,采用循环语句for handle in browser.window_handles: browser.switch_to.window(handle)重新定位界面。打开NGA-West2数据库后,先分别用find_element_by_name函数输入登录名和密码的name值定位,再用send.key函数输入登陆名和密码。最后同样用find_element_by_xpath函数点击Submit进入下载界面。

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1561707770257 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

第三步下载制定RSN编号的数据库。同样首先定位到RSN编号输入框,然后依次将RSN编号输入。接着点击Search Records进入下载界面。此时网页会跳出提示对话框,采用WebDriverWait函数等待浏览器弹出对话框再点击确定即可下载。

相关文章

网友评论

    本文标题:python网页爬虫实战:PEER数据库地震波批量下载 !

    本文链接:https://www.haomeiwen.com/subject/ixfecctx.html