-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
464a9c5
commit 90cc3ae
Showing
25 changed files
with
600,849 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38,192 changes: 38,192 additions & 0 deletions
38,192
11-regularni-izrazi/250-najbolj-znanih-filmov.html
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import re | ||
|
||
with open("podatki-o-filmih.html") as f: | ||
vsebina = f.read() | ||
|
||
def rocno_poisci_vse_pojavitve(niz: str, vzorec): | ||
zacetek = 0 | ||
while True: | ||
zacetek = niz.find(vzorec, zacetek + len(niz)) | ||
if zacetek == -1: | ||
break | ||
yield zacetek | ||
|
||
def poisci_vse_pojavitve(niz: str, vzorec): | ||
for m in re.finditer(vzorec, niz): | ||
yield m.start(), m.end() | ||
|
||
def vse_pojavitve(niz, vzorec, kontekst=20): | ||
for zacetek, konec in poisci_vse_pojavitve(niz, vzorec): | ||
print(niz[zacetek - kontekst:konec + kontekst]) | ||
print(kontekst * ' ' + (konec - zacetek) * '^') | ||
|
||
|
||
count = 0 | ||
for film in re.finditer(r'<a href="/title/tt(?P<id>\d+)/\?ref_=adv_li_tt">(?P<naslov>.*)</a>', vsebina): | ||
count += 1 | ||
print(film.groupdict()) | ||
print(count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import re | ||
|
||
with open("250-najbolj-znanih-filmov.html") as dat: | ||
vsebina = dat.read() | ||
|
||
vzorec = re.compile( | ||
r'<a href="/title/tt' | ||
r'(?P<id>\d+)' | ||
r'/\?ref_=adv_li_tt">(?P<naslov>.+?)</a>\s*' | ||
r'<span class="lister-item-year text-muted unbold">' | ||
r'(\([IVXLCDM]+\) )?' # če je več filmov v istem letu, dobijo rimske številke | ||
r'\((?P<leto>\d+)\)' # vzorec za leto | ||
r'</span>' | ||
) | ||
|
||
for i, ujemanje in enumerate(vzorec.finditer(vsebina), 1): | ||
print(i, ujemanje.groupdict()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import bs4, re | ||
|
||
with open('250-najbolj-znanih-filmov.html') as f: | ||
vsebina = f.read() | ||
|
||
zupa = bs4.BeautifulSoup(vsebina, 'html.parser') | ||
|
||
count = 0 | ||
for povezava in zupa.find_all('a'): | ||
link = povezava.get('href') | ||
if link and link.endswith('?ref_=adv_li_tt'): | ||
naslov = povezava.string | ||
id = int(re.search('\d{7}', link).group(0)) | ||
znacka_z_letom = povezava.find_next_sibling() | ||
niz_z_letom = znacka_z_letom.string | ||
vzorec_leta = r'(\([IVXLCDM]+\) )?\((?P<leto>.*?)\)' | ||
leto = int(re.fullmatch(vzorec_leta, niz_z_letom).group('leto')) | ||
print({'id': id, 'naslov': naslov, 'leto': leto}) | ||
count += 1 | ||
print(count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import re | ||
|
||
vzorec_bloka = re.compile( | ||
r'<div class="lister-item mode-advanced">.*?' | ||
r'</p>\s*</div>\s*</div>', | ||
flags=re.DOTALL | ||
) | ||
|
||
vzorec_filma = re.compile( | ||
r'<a href="/title/tt(?P<id>\d+)/.*?".*?' | ||
r'img alt="(?P<naslov>.+?)".*?' | ||
r'lister-item-year text-muted unbold">.*?\((?P<leto>\d{4})\)</span>.*?' | ||
r'runtime">(?P<dolzina>\d+?) min</.*?' | ||
r'<span class="genre">(?P<zanri>.*?)</span>.*?' | ||
r'<strong>(?P<ocena>.+?)</strong>.*?' | ||
r'<p class="text-muted">(?P<opis>.+?)</p.*?' | ||
r'Directors?:(?P<reziserji>.+?)(<span class="ghost">|</p>).*?' | ||
r'Votes:.*?data-value="(?P<glasovi>\d+)"', | ||
flags=re.DOTALL | ||
) | ||
|
||
vzorec_osebe = re.compile( | ||
r'<a\s+href="/name/nm(?P<id>\d+)/?[^>]*?>(?P<ime>.+?)</a>', | ||
flags=re.DOTALL | ||
) | ||
|
||
vzorec_povezave = re.compile( | ||
r'<a.*?>(.+?)</a>', | ||
flags=re.DOTALL | ||
) | ||
|
||
vzorec_zasluzka = re.compile( | ||
r'Gross:.*?data-value="(?P<zasluzek>(\d|,)+)"', | ||
flags=re.DOTALL | ||
) | ||
|
||
vzorec_metascore = re.compile( | ||
r'<span class="metascore.*?">(?P<metascore>\d+)', | ||
flags=re.DOTALL | ||
) | ||
|
||
vzorec_oznake = re.compile( | ||
r'<span class="certificate">(?P<oznaka>.+?)</span>', | ||
flags=re.DOTALL | ||
) | ||
|
||
vzorec_daljsi_povzetek = re.compile( | ||
r'<a href="/title/tt\d+/plotsummary.*? »', | ||
flags=re.DOTALL | ||
) | ||
|
||
vzorec_igralcev = re.compile( | ||
r'Stars?:(?P<igralci>.+?)</p>.*?', | ||
flags=re.DOTALL | ||
) | ||
|
||
|
||
def izloci_osebe(niz): | ||
osebe = [] | ||
for oseba in vzorec_osebe.finditer(niz): | ||
osebe.append({ | ||
'id': int(oseba.groupdict()['id']), | ||
'ime': oseba.groupdict()['ime'], | ||
}) | ||
return osebe | ||
|
||
|
||
def izloci_podatke_filma(blok): | ||
film = vzorec_filma.search(blok).groupdict() | ||
film['id'] = int(film['id']) | ||
film['dolzina'] = int(film['dolzina']) | ||
film['zanri'] = film['zanri'].strip().split(', ') | ||
film['leto'] = int(film['leto']) | ||
# odstranimo morebitno povezavo na daljši posnetek | ||
film['opis'] = vzorec_daljsi_povzetek.sub('', film['opis']) | ||
# odstranimo morebitne povezave v opisu | ||
film['opis'] = vzorec_povezave.sub(r'\1', film['opis']) | ||
film['opis'] = film['opis'].strip() | ||
film['ocena'] = float(film['ocena']) | ||
film['glasovi'] = int(film['glasovi']) | ||
film['reziserji'] = izloci_osebe(film['reziserji']) | ||
# zabeležimo oznako, če je omenjena | ||
oznaka = vzorec_oznake.search(blok) | ||
if oznaka: | ||
film['oznaka'] = oznaka['oznaka'] | ||
else: | ||
film['oznaka'] = None | ||
# zabeležimo igralce, če so omenjeni | ||
igralci = vzorec_igralcev.search(blok) | ||
if igralci: | ||
film['igralci'] = izloci_osebe(igralci['igralci']) | ||
else: | ||
film['igralci'] = [] | ||
# zabeležimo zaslužek, če je omenjen | ||
zasluzek = vzorec_zasluzka.search(blok) | ||
if zasluzek: | ||
film['zasluzek'] = int(zasluzek['zasluzek'].replace(',', '')) | ||
else: | ||
film['zasluzek'] = None | ||
# zabeležimo metascore, če je omenjen | ||
metascore = vzorec_metascore.search(blok) | ||
if metascore: | ||
film['metascore'] = int(metascore['metascore']) | ||
else: | ||
film['metascore'] = None | ||
return film | ||
|
||
with open('250-najbolj-znanih-filmov.html') as f: | ||
vsebina = f.read() | ||
|
||
count = 0 | ||
for blok in vzorec_bloka.finditer(vsebina): | ||
print(izloci_podatke_filma(blok.group(0))) | ||
count += 1 | ||
print(count) |
Oops, something went wrong.