-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_mpi.py
More file actions
177 lines (138 loc) · 4.55 KB
/
parser_mpi.py
File metadata and controls
177 lines (138 loc) · 4.55 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from asyncio import Protocol
from datetime import datetime
from urllib import response
import requests
from bs4 import BeautifulSoup
import re
MAIN_URL = "https://www.mpg.de/jobboard"
HEADERS = {"User-Agent": "Mozilla/5.0",
"X-Requested-With": "XMLHttpRequest"
}
def get_jobboard_id(session, MAIN_URL):
response = session.get(MAIN_URL, headers=HEADERS)
soup = BeautifulSoup(response.text, 'html.parser')
button = soup.select_one("a#more-job-offers")
if not button or not button.has_attr('href'):
raise ValueError("Could not find jobboard link")
href = button['href']
match = re.match(r"/jobboard/(\d+)/", href)
if not match:
raise ValueError("Could not extract jobboard id from href")
return match.group(1)
def fetch_initial_jobs(session, MAIN_URL):
response = session.get(MAIN_URL, headers=HEADERS)
soup = BeautifulSoup(response.text, 'html.parser')
job_items = soup.select("li.teaser")
jobs = []
for item in job_items:
title_element = item.select_one("h3 a")
if not title_element:
continue
title = title_element.get_text(strip=True)
link = "https://www.mpg.de" + title_element.get("href")
date = item.select_one(".date").get_text(strip=True)
institution = item.select("div")[-1].get_text(strip=True)
jobs.append({
"title": title,
"link": link,
"date": date,
"institution": institution,
})
return jobs
def fetch_more_jobs(session, base_api, start_offset=5, limit=5):
offset = start_offset
more_jobs = []
while True:
params = {
"region": "",
"subject": "",
"job_type": "",
"context": "jobs",
"limit": limit,
"offset": offset,
}
response = session.get(base_api, headers=HEADERS, params=params)
html = response.text.strip()
print(response.url)
print(base_api)
if not html:
print("No more jobs")
break
soup = BeautifulSoup(html, 'lxml')
job_items = soup.select("li.teaser")
if not job_items:
print("No more jobs")
break
for item in job_items:
title_element = item.select_one("h3 a")
if not title_element:
continue
title = title_element.get_text(strip=True)
print(title)
link = "https://www.mpg.de" + title_element.get("href")
date = item.select_one(".date").get_text(strip=True)
institution = item.select("div")[-1].get_text(strip=True) if item.select("div") else ""
more_jobs.append({
"title": title,
"link": link,
"date": date,
"institution": institution,
})
offset += limit
return more_jobs
# class Job:
# url: str
# money: float
# initial_post_time: datetime.datetime
#
# class FindJobs(Protocol):
# def find_jobs(self, session: str, url: str):
# ...
#
# class Parser1(FindJobs):
# def find_jobs(self, session: str, url: str):
# soup = BeautifulSoup(response.text, 'html.parser')
#
# class Parser2(FindJobs):
# def find_jobs(self, session: str, url: str):
# ...
# sites = {
# "site-one": Parser1,
# "site-two": Parser2,
# }
#
# class FilterJobs(Protocol):
# def filter_jobs(self, job: list[Job]) -> list[Job]:
# ...
#
# class FilterByTime(FilterJobs):
# def filter_jobs(self, job: list[Job]) -> list[Job]:
# return [job if job.initial_post_time < datetime.datetime.now()]
#
# class FilterByKeyword(FilterJobs):
# def filter_jobs(self, job: list[Job]) -> list[Job]:
# return [j for j in jobs if j.url == "a"]
#
# def funt(test):
# ...
#
# funt(FilterJobs.filter_jobs())
def main():
session = requests.Session()
list_id = get_jobboard_id(session, MAIN_URL)
base_api = f"https://www.mpg.de/jobboard/{list_id}/more_items"
# fetch initial jobs
initial_jobs = fetch_initial_jobs(session, f"{MAIN_URL}")
# fetch more jobs
more_jobs = fetch_more_jobs(session, base_api, start_offset=len(initial_jobs), limit=5)
all_jobs = initial_jobs + more_jobs
print(f"Found {len(all_jobs)} jobs")
if __name__ == "__main__":
main()
# sites = ["site-one"]
# jobs = []
# for site in sites:
# parser = sites.get(site)
# jobs += parser.find_jobs("test", f"{MAIN_URL}/{site}")
# filter = get_filter()
# filter.filter_jobs(jobs)