Please help me, I did a parsing of the site and wanted to collect the words in English and their translation and put them in a json file, but when saving these words in a json file, they begin to repeat themselves and are underlined in red. Here is my code:
import requests
from bs4 import BeautifulSoup
import json
headers = {
'accept': '*/*',
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"
}
def get_data():
#req = requests.get(url='https://english4life.ru/anglijskie-slova-po-temam-s-proiznosheniem', headers=headers)
with open("index.html") as file:
src = file.read()
soup = BeautifulSoup(src, "lxml")
href = soup.find("div", class_="su-row").find_all("a")
projects_url = []
for hrefs in href:
project_url = hrefs.get("href")
projects_url.append(project_url)
project_data_list = []
for project_url in projects_url[0:5]:
req = requests.get(project_url, headers)
project_name = project_url.split('/')[-1]
with open(f'data/{project_name}', "w") as file:
file.write(req.text)
with open(f"data/{project_name}") as file:
src = file.read()
soup = BeautifulSoup(src, "lxml")
project_data = soup.find("div", class_='entry-con')
try:
project_English = project_data.find('tbody').find_all("td", class_='column-1')
for projects_English in project_English:
print(projects_English.text)
except Exception:
print("No")
try:
project_translate = project_data.find('tbody').find_all("td", class_='column-2')
for projects_translate in project_translate:
print(projects_translate.text)
except Exception:
print("no")
project_data_list.append(
{
'Англиское слово:': projects_English.text,
'Перевод:': projects_translate.text
}
)
with open("project_data.json", "a", encoding=('utf-8')) as file:
json.dump(project_data_list, file, indent=4, ensure_ascii=False)
get_data()