老家发生地址了,微信群里出现各种谣言,所以爬一下历年地震数据,辟谣用#爬取数据
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()
#之后弄一个图表
来源:https://www.cnblogs.com/lijifei/p/12165916.html