Linguee_webscraper/main.py
2024-04-01 01:44:41 +02:00

46 lines
1.4 KiB
Python
Executable File

#!/usr/bin/python3
import requests
from lxml import html
import string
charset = string.ascii_lowercase
url = 'https://www.linguee.fr/francais-anglais/search'
params = {
'ch': 0
}
MAXIMUM_SUGGESTIONS = 4
entries = set()
def treatSuffixes(prefix):
#print(prefix)
for char in charset:
base = prefix + char
print(base)
params['qe'] = base
text = requests.get(url, params = params).text
# Pay attention if change `base` elaboration to not allow unwanted folder file writing.
with open(f'requests/{base}.html', 'w') as requestFile:
requestFile.write(text)
tree = html.fromstring(text)
rows = tree.xpath('//div[@class="main_row"]')
rowsLen = len(rows)
assert rowsLen <= MAXIMUM_SUGGESTIONS, f'More than {MAXIMUM_SUGGESTIONS} rows!'
interestingEntries = True
for row in rows:
item = row.xpath('div[@class="main_item"]')[0]
entry = item.text_content()
wordType = row.xpath('div[@class="main_wordtype"]')
if wordType != []:
if item.attrib['lc'] == 'FR' and not entry in entries:
print(len(entries), entry, wordType[0].text_content())
entries.add(entry)
if not base in entry:
interestingEntries = False
if rowsLen == MAXIMUM_SUGGESTIONS and interestingEntries:
treatSuffixes(base)
treatSuffixes('')