爬取湖北历年地震数据

丶灬走出姿态 提交于 2020-01-08 13:33:01
老家发生地址了,微信群里出现各种谣言,所以爬一下历年地震数据,辟谣用#爬取数据
import requests
from bs4 import BeautifulSoup


def get_header():
    header_dic = {
        'User-Agent': 'Mozilla / 5.0',
    }

def get_page():
    url_list = page_urls()
    for url in url_list:
        # print(url)
        res = requests.get(url=url,headers=get_header())
        if res.status_code==200:
            soup = BeautifulSoup(res.text,'html.parser')
            equ_info_gover = soup.find(class_="gover_right_ul").find_all('li')
            for equ_info in equ_info_gover:
                equ_time = equ_info.span.text
                equ_des = equ_info.a.text
                # print(equ_time,equ_des)
                equ_time_des = equ_info.span.text +','+ equ_info.a.text + '\n'
                write_to_file(equ_time_des)
        else:
            print(url)

def write_to_file(equ_time_des):
    with open('equ_hubei_info.txt','a',encoding='utf8') as f:
        f.write(equ_time_des)
        f.close()



def page_urls():
    url_list = []
    for page_num in range(1,13):
        url = 'http://www.eqhb.gov.cn/list_map.jsp?equ7t=12&equ7p='+ str(page_num) +'&equ7c=15&urltype=tree.TreeTempUrl&wbtreeid=1008'
        url_list.append(url)
    return url_list


if __name__ == '__main__':
    get_page()

  #之后弄一个图表

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!