必威体育Betway必威体育官网
当前位置:首页 > IT技术

抓取中国地震台网最近一年地震数据,生成表格以及地震分布图

时间:2019-10-11 02:12:15来源:IT技术作者:seo实验室小编阅读:87次「手机版」
 

地震带分布图

代码如下:

import requests
from lxml import etree
import pymysql
from pyecharts import Geo
import numpy as np

earthquake_n = []
earthquake_t = []
earthquake_location_lat = []
earthquake_location_lon = []
earthquake_location = []
earthquake_url = []
earthquake_deapth = []
shuju = []
geo_lacation = {}
geo_data = []
geo_attrs = []
geo_values = []


def Get_html(url: object, params: object) -> object:
    header = {'User-Agent': 'Mozilla/5.0'}
    params = params
    r = requests.get(url, params=params, headers=header)
    print(r.url)
    if r.status_code == 200:
        r.encoding = r.APParent_encoding
        # print(r.text)
        html = r.text
    else:
        print("网页爬取异常")
        html = "网页爬取异常"
    return (html)


def Get_data(html):
    html = etree.HTML(html)
    trs = html.xpath("//p[@class='title-content']/p[@class='speedquery']/p[@id='speed-search']/table["
                     "@class='speed-table1']/tr")
    print(trs)
    for tr in trs:
        earthquake_m1 = tr.xpath("./td[1]/text()")
        earthquake_t1 = tr.xpath("./td[2]/text()")
        earthquake_location_lat1 = tr.xpath("./td[3]/text()")
        earthquake_location_lon1 = tr.xpath("./td[4]/text()")
        earthquake_deapth1 = tr.xpath("./td[5]/text()")
        earthquake_location1 = tr.xpath("./td[6]/a/text()")
        earthquake_url1 = tr.xpath("./td[6]/a/@href")
        print(earthquake_m1, earthquake_t1, earthquake_location_lat1, earthquake_location_lon1, earthquake_deapth1,
              earthquake_location1, earthquake_url1)
        try:
            earthquake_n.append(earthquake_m1[0])
            earthquake_t.append(earthquake_t1[0])
            earthquake_location_lat.append(earthquake_location_lat1[0])
            earthquake_location_lon.append(earthquake_location_lon1[0])
            earthquake_deapth.append(earthquake_deapth1[0])
            earthquake_location.append(earthquake_location1[0])
            earthquake_url.append(earthquake_url1[0])

        except:
            print("异常")
    for i in range(0, len(earthquake_n) - 1):
        shuju.append((earthquake_n[i], earthquake_t[i], earthquake_location_lat[i], earthquake_location_lon[i],
                      earthquake_deapth[i], earthquake_location[i], earthquake_url[i]))
    print(shuju)


def Mysql_create_table(name):
    client = pymysql.connect(user="root", host="localhost", passwd="*******", db="xiaolimao")
    cursor = client.cursor()
    sql = "create table if not exists table_%s" % name + "(earthquake_n VARCHAR(100),earthquake_t VARCHAR(100),earthquake_location_lat VARCHAR(100)" \
                                                         ", earthquake_location_lon VARCHAR(100),earthquake_deapth VARCHAR(100),earthquake_location VARCHAR(200)," \
                                                         "earthquake_url VARCHAR(100));"
    cursor.execute(sql)
    cursor.close()
    client.close()


def Mysql_data(name, shuju):
    client = pymysql.connect(user="root", host="localhost", passwd="*******", db="xiaolimao")
    cursor = client.cursor()
    sql = "insert into table_%s" % name + " values(%s,%s,%s,%s,%s,%s,%s)"
    cursor.executemany(sql, shuju)
    client.commit()
    cursor.close()
    client.close()


def Num_int():
    for i in range(len(earthquake_n) - 1):
        geo_lacation[(earthquake_location[i])] = [float(earthquake_location_lon[i]), float(earthquake_location_lat[i])]
        geo_attrs.append(earthquake_location[i])
        geo_values.append(float(earthquake_n[i]))
    print(geo_lacation, geo_attrs, geo_values)


def Geo_chart():
    Num_int()
    geo = Geo("最近一年地震分布图", "data from 51job", title_color="#fff", title_pos="center", width=1200, height=60, background_color='#404a59')
    attr = geo_attrs
    value = geo_values
    geo.add("", attr, value, visual_range=[0, np.max(value)], maptype="world", visual_text_color="#fff", symbol_size=15,
            is_visualmap=True, geo_cities_coords=geo_lacation)
    geo.show_config()
    geo.render()


if __name__ == "__main__":
    url = "http://www.ceic.ac.cn/speedsearch"
    name = input("表名:")
    for i in range(1, int(input("请输入大于1的整数:"))):
        params = {"time": 6, "page": i}
        html = Get_html(url, params)
        Get_data(html)
    Mysql_create_table(name)
    Mysql_data(name, shuju)
    Geo_chart()

结果:

相关阅读

天猫家年华优惠券怎么领?天猫家年华活动时间是多久?

天猫家年华优惠券怎么领?天猫家年华活动时间是多久?天猫家年华居家狂欢活动期间,天猫会发放大量的天猫家年华优惠券,那么天猫家年华

建行支付宝卡通年费

随着,更多的人选择网上开店创业,选择银行卡、开通网银就是必须的步骤,于是很多人就开始关心各个银行的网银费用问题,今天,如何开网店

2018年PMP项目管理专业人士资格认证考试回顾总结

本文最后修改时间:2019年3月26日 19:22 前言 以前人们常说,要想升职,就得经过PMP(拍马屁)。而信息科技时代到来了,颠覆了PMP这一词的含

怎么看网易云音乐 2018 年年度报告?

这是一个聪明但我不太喜欢的运营活动,低于我对网易云音乐的预期。聪明为什么说它聪明呢?因为必然稳定有成绩的同时还有创新。稳定而

走过十年的新浪微博,还能走到下一个十年吗?

文/东方亦落今年是新浪微博十周年,新浪董事长曹国伟向全体员工发布了内部信,表示“微博的成功让新浪赶上了移动互联网大潮,也使

分享到:

栏目导航

推荐阅读

热门阅读