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

代码如下:

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("//div[@class='title-content']/div[@class='speedquery']/div[@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()

结果:

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值