forked from szentjozsefhackathon/sematizmus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHdFEMScraper.py
74 lines (62 loc) · 2.52 KB
/
HdFEMScraper.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
import requests
from bs4 import BeautifulSoup
import re
from tqdm import tqdm
import json
import argparse
def HdFEM(filename=None, year=None):
# Replace this with the URL of the website you want to scrape
url = 'https://hd.gorogkatolikus.hu/adattar-papjaink'
response = requests.get(url, verify=False)
# Check if the request was successful
if response.status_code == 200:
html_content = response.content
else:
print("Failed to fetch the website.")
# Parse the HTML content with BeautifulSoup
soup = BeautifulSoup(html_content, 'html.parser')
papok = []
for pap in soup.select(".adattar_cont"):
papok.append(pap['onclick'][15:-1])
for pap in soup.select(".adattar_cont_left"):
papok.append(pap['onclick'][15:-1])
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://hd.gorogkatolikus.hu/{pap}", verify=False)
if response.status_code == 200:
html_content = response.content
else:
print("Failed to fetch the website.")
except:
try:
response = requests.get(f"https://hd.gorogkatolikus.hu/{pap}", verify=False)
if response.status_code == 200:
html_content = response.content
else:
print("Failed to fetch the website.")
except:
print("Big error")
continue
soup = BeautifulSoup(html_content, 'html.parser')
imgSrc = ""
try:
imgSrc = "https://hd.gorogkatolikus.hu/" + soup.select_one(".kep-terkep img").get("src")
except:
pass
paplista.append({
"name": soup.select_one(".aloldal_cim").text, # A pap neve
"img": imgSrc, # A kép linkje,
"src": f"https://hd.gorogkatolikus.hu/{pap}"
})
if filename == None: return paplista
else:
with open(filename, "w") as outfile:
outfile.write(json.dumps(paplista))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Hajdúdorogi főegyházmegye 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(HdFEM(args.filename))
else: HdFEM(args.filename)