-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (57 loc) · 1.76 KB
/
main.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
# "www.verbformen.de"
# verbs = ["machen"]
import csvinterface as csvi
import re
import requests
from bs4 import BeautifulSoup as bs
# PARSES ONE CONJUGATION OF A CERTAIN TENSE
def trtext(tr)->str:
outstr = []
for td in tr.find_all("td"):
for item in td.contents:
notags = re.sub("<.*?>", "", str(item))
outstr.append(notags)
return "".join(outstr)
def get_conjugations(verb: str)->list:
# sleep(1)
url = f"https://www.verbformen.de/konjugation/?w={verb}"
r = requests.get(url)
soup = bs(r.text, "html5lib")
mainsection = ""
maindiv = soup.find_all("div", attrs={"class": "rAbschnitt"})
for section in maindiv[0].find_all("section"):
verbtype = section.find("header").find("h2")
if verbtype is not None and verbtype.text == "Indikativ":
mainsection = section
break
else:
return []
# ITER THRU EACH TENSE
conjugs = []
for table in mainsection.find_all("div", class_="vTbl"):
for tr in table.find_all("tr"):
conjugs.append(trtext(tr))
return conjugs
infinitives = csvi.verbreturns()
bigverbs = []
zed = 0
from time import sleep
from random import randint
for verb in infinitives:
try:
oneVerbConjugs = get_conjugations(verb)
# print(oneVerbConjugs)
if len(oneVerbConjugs) == 0:
continue
oneVerbConjugs.insert(0, verb)
# print(oneVerbConjugs)
zed += 1
csvi.appverbs([oneVerbConjugs])
print(f"successfully conjugated {verb}")
# bigverbs.append(oneVerbConjugs)
except:
print(f"Err on {verb}")
sleep(randint(2, 6))
# "https://www.verbformen.de/konjugation/?w=machen"
# csvi.appverbs(bigverbs)
print(f"Conjugated {zed} verbs")