Приветствую.
Есть код парсинга. Результат нужно сохранить в csv. Выдает ошибку : KeyError: 'name'. Проверил на предмет правильности сбора данных, сохранив в json. Все корректно сохраняет. Вот код. Подскажите, в чем проблема.
with open('all_links.txt', 'r', encoding='utf-8') as f:
lines = [line.strip() for line in f.readlines()]
companys = []
for line in lines:
q = requests.get(line)
result = q.content
soup = BeautifulSoup(result, 'lxml')
try:
name = soup.find('h1', class_='detailPrimaryTitle').text
except AttributeError:
name = '-'
try:
web = soup.find('div', class_='detailBasicInfo').find(attrs={'data-dot':'show-website'}).get_text()
except AttributeError:
web = '-'
try:
phone1 = soup.find('div', class_='value detailPhone detailPhonePrimary').text
except AttributeError:
phone1 = '-'
try:
phone2 = soup.find('div', class_='value detailPhone noLabel').text
except AttributeError:
phone2 = '-'
try:
phone3 = soup.find('div', class_='value detailPhone').text
except AttributeError:
phone3 = '-'
try:
mail = soup.find('div', class_='value detailEmail').text
except AttributeError:
mail = '-'
try:
location = soup.find('div', class_='detailAddress').text
except AttributeError:
location = '-'
try:
ident_nmb = soup.find('div', class_='value detailBusinessInfo').text.split('Více')[0]
except AttributeError:
ident_nmb = '-'
try:
description = soup.find(class_='description').text
except AttributeError:
description = '-'
company = {
'name': name,
'web': web,
'phone1': phone1,
'phone2': phone2,
'phone3': phone3,
'mail': mail,
'location': location,
'ident_nmb': ident_nmb,
'description': description
}
companys.append(company)
with open('companys.csv', 'w', newline='') as file:
writer = csv.writer(file, delimiter=';')
writer.writerow(['Name', 'Web', 'Phone1', 'Phone2', 'Phone3', 'Mail', 'Location', 'Ident_nmb', 'Description'])
for line in lines:
writer.writerow(
{soup['name'], soup['web'], soup['phone1'], soup['phone2'], soup['phone3'], soup['mail'], soup['location'],
soup['ident_nmb'], soup['description']})