-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKOScraper.py
152 lines (134 loc) · 5.24 KB
/
KOScraper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import requests
from bs4 import BeautifulSoup
import re
from tqdm import tqdm
import json
import argparse
import datetime
from deleteDr import deleteDr
honapok = {
"január": 1,
"február": 2,
"március": 3,
"április": 4,
"május": 5,
"június": 6,
"július": 7,
"augusztus": 8,
"szeptember": 9,
"október": 10,
"november": 11,
"december": 12,
"01": 1,
"02": 2,
"03": 3,
"04": 4,
"05": 5,
"06": 6,
"07": 7,
"08": 8,
"09": 9,
"10": 10,
"11": 11,
"12": 12
}
def str2date(datum):
reszek = [d.split(".")[0].strip() for d in datum.strip().split(" ")]
return datetime.date(int(reszek[0]), honapok[reszek[1]], int(reszek[2]))
@deleteDr
def KO(filename=None, year=None):
# Replace this with the URL of the website you want to scrape
url = 'https://ktp.hu/js/ajax/listazo.php?url=lelkeszek%2Ftabori-lelkeszi-kar&fm=110&am=173'
response = requests.get(url, verify=False)
# Check if the request was successful
if response.status_code == 200:
html_content = response.content
else:
print(f"{url} - Failed to fetch the website.")
# Parse the HTML content with BeautifulSoup
soup = BeautifulSoup(html_content, 'html.parser')
papok = []
for sor in soup.findAll("a"):
papok.append(sor['href']) # Papi oldalak linkjei
papok.append("/lelkeszek/takacs-tamas")
papok.append("/lelkeszek/tabori-puspok/eletrajz")
paplista = []
for pap in tqdm(papok): # Nézze meg az összes pap linkjét
try: # Kétszeri próbálkozásra szokott menni
response = requests.get(f"https://ktp.hu{pap}", verify=False)
if response.status_code == 200:
html_content = response.content
else:
print(f"https://ktp.hu{pap} - Failed to fetch the website.")
except:
try:
response = requests.get(f"https://ktp.hu{pap}", verify=False)
if response.status_code == 200:
html_content = response.content
else:
print(f"https://ktp.hu{pap} - Failed to fetch the website.")
except:
print(f"https://ktp.hu{pap} - Big error")
continue
soup = BeautifulSoup(html_content, 'html.parser')
imgSrc = ""
try:
imgSrc = "https://ktp.hu" + soup.select_one(".bordo img").get("src")
except:
pass
name = ""
if pap == "/lelkeszek/tabori-lelkeszi-kar/mikus-tibor":
name = "Mikus Tibor"
elif pap == "/lelkeszek/tabori-puspok/eletrajz":
name = "Berta Tibor"
imgSrc = "https://ktp.hu/uploads/content/177/kepek/c_letoltes.png"
else:
name = soup.select_one(".bordo:first-child").text
delimiter = '###' # unambiguous string
for line_break in soup.findAll('br'): # loop through line break tags
line_break.replaceWith(delimiter) # replace br tags with delimiter
strings = soup.get_text().split(delimiter) # get list of strings
birth = None
ordination = None
for row in soup.get_text().split(delimiter):
for subrow in row.split("\n"):
if "Születési helye és ideje:" in subrow or "Születési helye, ideje:" in subrow:
try:
birth = str2date(subrow.split(",")[-1].replace(".", ". ").replace(" "," ").strip())
except:
print(soup.get_text().split(delimiter))
print(f'https://ktp.hu{pap} - {subrow}')
if "Pappá szentelés helye, ideje:" in subrow or "Pappá szentelés helye és ideje:" in subrow:
try:
ordination = str2date(subrow.split(",")[-1].replace(".", ". ").replace(" "," ").strip())
except:
print(soup.get_text().split(delimiter))
print(f'https://ktp.hu{pap} - {subrow}')
paplista.append({
"name": name.title().strip(), # A pap neve
"img": imgSrc, # A kép linkje,
"birth": birth,
"ordination": ordination,
"bishop": name == "Berta Tibor",
"src": f"https://ktp.hu{pap}"
})
paplista.append({
"name": "Bíró László",
"img": "https://ktp.hu/uploads/content/196/kepek/c_biro.jpg",
"birth": datetime.date(1950,10,31),
"ordination": datetime.date(1974,6,23),
"bishop": True,
"retired": True,
"src": "https://ktp.hu/lelkeszek/tabori-puspok/elodok/biro-laszlo"
})
if filename == None: return paplista
else:
with open(filename, "w") as outfile:
outfile.write(json.dumps(paplista, default=str))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Tábori püspökség papjainak adatai')
parser.add_argument('--filename', required=False, action="store", default=None, help="JSON to save. If not set, the result will be displayed on screen")
args = parser.parse_args()
if args.filename==None: print(KO(args.filename))
else: KO(args.filename)