├── .gitignore ├── LICENSE.txt ├── README.md ├── db.py ├── getgraveids.py └── input.txt /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | tree.ged 3 | graves.db 4 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2016 Robert Pirtle 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # scrape-a-grave 2 | 3 | > Scrape and Retrieve [FindAGrave](http://findagrave.com) memorial page data and save them to an SQL database. 4 | 5 | 6 | ## Scraping 7 | [FindAGrave](http://findagrave.com) is an index of gravemarkers from cemeteries around the world. Often when doing genealogy research, you don't want to rely on a webpage's future and so you want to download the information to your local file. This python script takes a list of Grave Marker numbers, or FindAGrave urls, scrapes the site for data and prints out a citation of the information. It is currently setup to also save the data in an SQL database. 8 | 9 | 10 | ## Requirements 11 | 12 | You are expected to have [Python3](https://www.python.org/downloads/). It also requires the BeautifulSoup package, downloadable through pip: 13 | ```sh 14 | $ pip3 install bs4 15 | ``` 16 | 17 | ## Usage 18 | Download these files and change the contents of input text to be a list of FindAGrave ids, or FindAGrave urls. Then run 19 | ```sh 20 | $ python3 getgraveids.py 21 | ``` 22 | 23 | The citations will be printed to the console and saved in an SQL database named `graves.db`. 24 | 25 | It is also possible to **read links from a GEDCOM** by un-highlighting the ["read from gedcom" section](https://github.com/PirtleShell/scrape-a-grave/blob/master/getgraveids.py#L88). This assumes your GEDCOM source citations have a LINK field with the FindAGrave site. 26 | 27 | 28 | ## License 29 | 30 | This is intended as a convenient tool for personal genealogy research. Please be aware of FindAGrave's [Terms of Service](https://secure.findagrave.com/terms.html). 31 | 32 | MIT © [Robert Pirtle](https://pirtle.xyz) 33 | -------------------------------------------------------------------------------- /db.py: -------------------------------------------------------------------------------- 1 | 2 | import sys 3 | import sqlite3 as sql 4 | 5 | def makeGraveDatabase(): 6 | conn = sql.connect('graves.db') 7 | c = conn.cursor() 8 | c.execute('''CREATE TABLE findAGrave 9 | (graveid INTEGER PRIMARY KEY, url TEXT, 10 | name TEXT, birth TEXT, birthplace TEXT, death TEXT, deathplace TEXT, 11 | burial TEXT, plot TEXT, more_info BOOL)''') 12 | conn.close() 13 | 14 | def addRowToDatabase(grave): 15 | row = (grave['id'],) 16 | keys = ['graveid'] 17 | for key in grave.keys(): 18 | if key == 'id': 19 | continue 20 | row += (grave[key],) 21 | keys.append(key) 22 | 23 | col_names = '(' + ', '.join(keys) + ')' 24 | value_hold = '(' + '?,' * (len(keys) - 1) + '?)' 25 | insert = 'INSERT INTO findAGrave ' + col_names + ' VALUES ' + value_hold 26 | 27 | try: 28 | conn = sql.connect('graves.db') 29 | c = conn.cursor() 30 | c.executemany(insert, [row]) 31 | conn.commit() 32 | conn.close() 33 | except: 34 | print('Memorial #' + grave['id'] + ' is already in database.') 35 | 36 | def extractBirth(grave, str): 37 | try: 38 | if 'Birthplace' in str: 39 | grave.update({'birth': str.split('\n')[0]}) 40 | grave.update({'birthplace': str.split('\n')[1].replace('Birthplace: ', '')}) 41 | else: 42 | grave.update({'birth': str}) 43 | except: 44 | print('error:', sys.exc_info()) 45 | 46 | def extractDeath(grave, str): 47 | if 'Death place:' in str: 48 | grave['death'] = str.split('\n')[0] 49 | grave['deathplace'] = str.split('\n')[1].replace('Death place: ', '') 50 | else: 51 | grave['death'] = str 52 | -------------------------------------------------------------------------------- /getgraveids.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os.path 3 | import urllib.request 4 | import sqlite3 as sql 5 | from bs4 import BeautifulSoup 6 | from db import makeGraveDatabase, addRowToDatabase, extractBirth, extractDeath 7 | 8 | problemchilds = [] 9 | CONNECT = True 10 | 11 | if CONNECT: 12 | if not os.path.isfile('./graves.db'): 13 | makeGraveDatabase() 14 | 15 | 16 | def findagravecitation(graveid): 17 | grave = {} 18 | grave['id'] = graveid 19 | 20 | url = 'http://www.findagrave.com/cgi-bin/fg.cgi?page=gr&GRid=' 21 | url += str(graveid) 22 | grave['url'] = url 23 | 24 | with urllib.request.urlopen(url) as html: 25 | soup = BeautifulSoup(html.read(), "lxml") 26 | text = 'Find A Grave Memorial #' 27 | text += str(graveid) + '\nName: ' 28 | name = soup.table.tr.td.find_next_siblings('td')[1].table.tr.get_text() 29 | text += name + '\nBirth: ' 30 | grave['name'] = name 31 | 32 | # a sponsored grave has an extra row 33 | checkSponsor = soup.table.tr.td.find_next_siblings('td')[1].table.tr.find_next_siblings('tr') 34 | 35 | if "Birth" in checkSponsor[1].get_text(): 36 | datatable = checkSponsor[1].td.tr.table.tr.find_all('tr') 37 | else: 38 | datatable = checkSponsor[2].td.tr.table.tr.find_all('tr') 39 | 40 | birth = datatable[0].find_all('td')[1] 41 | for i,br in enumerate(birth.find_all('br')): 42 | if i==0: br.replace_with('\nBirthplace: ') 43 | else: br.replace_with(', ') 44 | text += birth.get_text() +'\nDeath: ' 45 | 46 | extractBirth(grave, birth.get_text()) 47 | 48 | death = datatable[1].find_all('td')[1] 49 | for i,br in enumerate(death.find_all('br')): 50 | if i==0: br.replace_with('\nDeath place: ') 51 | else: br.replace_with(', ') 52 | text += death.get_text() +'\n' 53 | 54 | extractDeath(grave, death.get_text()) 55 | 56 | burialBox = datatable[4].td 57 | for i,br in enumerate(burialBox.find_all('br')): 58 | br.replace_with('\n') 59 | blines = burialBox.get_text().split('\n'); 60 | try: 61 | burial = blines[2] 62 | grave['burial'] = burial 63 | 64 | text += "Burial: " + burial 65 | for line in blines[3:]: 66 | if "Plot" in line: 67 | text += '\n' + line 68 | grave['plot'] = line.replace('Plot: ','') 69 | elif line != '': text += ', ' + line 70 | except: 71 | text += burial.get_text() 72 | problemchilds.append(graveid) 73 | 74 | if datatable[2].get_text() != '': 75 | text += '\nMore information available on webpage.' 76 | grave['more_info'] = True 77 | 78 | if CONNECT: 79 | addRowToDatabase(grave) 80 | 81 | return text 82 | 83 | 84 | graveids = [] 85 | numcites = 0 86 | numids = 0 87 | 88 | # # read from gedcom 89 | # with open('tree.ged', encoding='utf8') as ged: 90 | # for line in ged.readlines(): 91 | # numcites+=1 92 | # if '_LINK ' in line and 'findagrave.com' in line: 93 | # for unit in line.split('&'): 94 | # if 'GRid=' in unit: 95 | # if unit[5:-1] not in graveids: 96 | # graveids.append(unit[5:-1]) 97 | # #print(graveids[numids]) 98 | # numids+=1 99 | 100 | # read from text file 101 | with open('input.txt', encoding='utf8') as txt: 102 | for line in txt.readlines(): 103 | numcites+=1 104 | if 'findagrave.com' in line: 105 | for unit in line.split('&'): 106 | if 'GRid=' in unit: 107 | if unit[5:-1] not in graveids: 108 | graveids.append(unit[5:-1]) 109 | numids+=1 110 | elif line not in graveids: 111 | graveids.append(line) 112 | numids+=1 113 | 114 | parsed = 0 115 | failedids = [] 116 | for i,gid in enumerate(graveids): 117 | try: 118 | print(str(i+1) + ' of ' + str(numids)) 119 | print(findagravecitation(gid)+'\n\n') 120 | parsed += 1 121 | except: 122 | print('Unable to parse Memorial #'+str(gid)+'!\n\n') 123 | print("Error:", sys.exc_info()[0]) 124 | failedids.append(gid) 125 | 126 | out = 'Successfully parsed ' + str(parsed) + ' of ' 127 | out += str(len(graveids)) 128 | print(out) 129 | if len(problemchilds)>0: 130 | print('\nProblem childz were:', problemchilds) 131 | 132 | # with open('results.txt', 'w') as f: 133 | # f.write(out + '\n') 134 | # f.write('\nProblem childz were:\n') 135 | # f.write('\n'.join(problemchilds)) 136 | # f.write('\nUnable to parse:\n') 137 | # f.write('\n'.join(failedids)) 138 | -------------------------------------------------------------------------------- /input.txt: -------------------------------------------------------------------------------- 1 | https://secure.findagrave.com/cgi-bin/fg.cgi?page=gr&GRid=1075 2 | https://secure.findagrave.com/cgi-bin/fg.cgi?page=gr&GRid=534 3 | https://secure.findagrave.com/cgi-bin/fg.cgi?page=gr&GRid=574 4 | https://secure.findagrave.com/cgi-bin/fg.cgi?page=gr&GRid=627 5 | https://secure.findagrave.com/cgi-bin/fg.cgi?page=gr&GRid=544 6 | https://secure.findagrave.com/cgi-bin/fg.cgi?page=gr&GRid=6 7 | https://secure.findagrave.com/cgi-bin/fg.cgi?page=gr&GRid=7376621 8 | https://secure.findagrave.com/cgi-bin/fg.cgi?page=gr&GRid=95929698 9 | https://secure.findagrave.com/cgi-bin/fg.cgi?page=gr&GRid=1347 10 | --------------------------------------------------------------------------------