In [1]:
#Importing Libraries
import pandas as pd
import numpy as np

import datetime
import time
from datetime import datetime

import bs4
from bs4 import BeautifulSoup

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
In [2]:
#Dataframe creation
Data = pd.DataFrame(columns=['Date', 'Magnitude','Latitude','Longitude', 'Depth', 'Place'])
Dum_DF = pd.DataFrame(columns=['Date', 'Magnitude','Latitude','Longitude', 'Depth', 'Place'])

#Loop over each year
for k in range(30):
 
 year = 1900 + k
 print('Executing K: '+ str(k))   
 try:

    url = "https://earthquake.usgs.gov/fdsnws/event/1/query?format=xml&starttime=" + str(year) + "-01-01&endtime=" + str(year) + "-12-31&minmagnitude=5"
    
    browser = webdriver.PhantomJS(executable_path='D:\Anaconda Files\phantomjs')
    browser.get(url)
    time.sleep(40)
    html = browser.page_source
    time.sleep(40)
    browser.quit()
    html = html.encode("utf-8", 'ignore')
    soup = BeautifulSoup(html)

 except:
     print('Error ' + str(k))
        


 j=0
 i=0
 for i in soup.findAll("text"):
            
            #print('Executing J: '+ str(j)) 
            Place = str(soup.findAll("text")[j].getText())
            Latitude = soup.findAll("latitude")[j].getText()
            Longitude = soup.findAll("longitude")[j].getText()
 

            try:
             Magnitude_L1 = soup.findAll("mag")[j]
             Magnitude = Magnitude_L1.findAll("value")[0].getText()            

            except:
             Mag = 0
             print('Error Mag J:' +str(j))
            
            Date = soup.findAll("time")[j].getText()    
            
            try:
             Depth_L1 = soup.findAll("depth")[j]       
             Depth = Depth_L1.findAll("value")[0].getText()
            except:
             Depth = 0
             print('Error Depth K:' + str(k) + ' J:' +str(j))
                
            Dum_DF = pd.DataFrame( {'Date': [Date], 'Magnitude': [Magnitude], 
                                    'Latitude': [Latitude], 'Longitude': [Longitude], 'Depth': [Depth],
                                    'Place': [Place]})
            Data = Data.append(Dum_DF)
            j=j+1
            
Executing K: 0
D:\anaconda3\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py:49: UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
  warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
Error Depth K:0 J:316
Error Depth K:0 J:317
In [3]:
#SAve Dataframe
Data.reset_index(drop=True, inplace=True)
Data.to_csv(r'D:\Anaconda Files\Earthquakes\Earthquake_History6.csv',  decimal=',' , sep='\t', encoding='utf-8')
In [4]:
Data
Out[4]:
Date Magnitude Latitude Longitude Depth Place
0 1968-12-30T07:03:13.000Z 5.6 57.489 -151.445 33100 51 km ESE of Chiniak, Alaska
1 1968-12-22T16:44:44.000Z 5.6 56.331 -153.996 22700 69 km S of Akhiok, Alaska
2 1968-12-22T09:06:37.000Z 5.6 36.306 101.772 25000 35 km S of Xining, China
3 1968-12-19T16:30:01.060Z 5.52 37.2315 -116.4736667 1400 44km NE of Beatty, NV
4 1968-12-19T15:15:59.000Z 6 53.434 160.07 44100 104 km ENE of Petropavlovsk-Kamchatsky, Russia
... ... ... ... ... ... ...
313 1968-01-07T09:56:41.000Z 5.7 -5.042 153.961 15000 203 km ESE of Kokopo, Papua New Guinea
314 1968-01-06T23:27:23.000Z 6.4 -27.845 -70.873 26000 75 km SW of Copiapó, Chile
315 1968-01-04T10:27:39.000Z 5.8 -9.779 148.835 60000 130 km SSE of Popondetta, Papua New Guinea
316 1968-01-04T00:57:44.000Z 6.1 52.226 -171.468 0 186 km E of Atka, Alaska
317 1968-01-02T00:21:12.000Z 5.9 -5.175 153.421 0 157 km SE of Kokopo, Papua New Guinea

318 rows × 6 columns

In [ ]: