Есть скрипт, которые фильтрует опеределенные данные и добавляет их в таблицу, но иногда пропускает поля username и link_to_profile. Поля map_url добавляет всегда, а social_media если есть.
Обязательно должен добавлять username и link_to_profile, но пропускает, что делать?
import re
import time
from openpyxl import load_workbook
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
file = 'maps_makers.xlsx'
work_book = load_workbook(file)
ws = work_book['map_creators']
class MapCreator:
def __init__(self, username, map_url, link_to_profile, social_media):
self.username = username
self.map_url = map_url
self.link_to_profile = link_to_profile
self.social_media = social_media
def add_map_creator(self):
ws.append([self.username, self.map_url, self.link_to_profile, self.social_media])
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
excel_index = 2
url = "https://sc2arcade.com/map-list"
map_creators = set()
try:
driver.get(url)
time.sleep(5)
while True:
maps_names = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".title")))
for map_index in range(len(maps_names)):
maps_names = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".title")))
map_element = maps_names[map_index]
map_name = map_element.text
if re.search(r'[a-zA-Z а-яА-ЯёЁ]', map_name):
print(f"Processing map: {map_name}")
driver.execute_script("arguments[0].click();", map_element)
map_url = driver.current_url
try:
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".player-link > span")))
author_name = driver.find_element(By.CSS_SELECTOR, '.player-link').get_attribute('href')
driver.get(author_name)
username = driver.find_element(By.CSS_SELECTOR, '.h1.flex-grow-1.font-weight-regular').text
print(username)
link_to_profile = driver.current_url
driver.back()
print(link_to_profile)
social_media = driver.find_element(
By.XPATH,
'.//*[@id="app"]/div/main/div/div/div/div[1]/div[1]/dl/dd[5]/a/span'
).text.lower()
print(social_media)
except:
username = ""
link_to_profile = ""
social_media = ""
ws[f'A{excel_index}'] = f'{username}'
ws[f'F{excel_index}'] = map_url
ws[f'L{excel_index}'] = link_to_profile
ws[f'R{excel_index}'] = social_media
excel_index += 1
driver.get(url)
time.sleep(5)
else:
driver.execute_script("arguments[0].click();", map_element)
map_url = driver.current_url
try:
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".player-link > span")))
author_name = driver.find_element(By.CSS_SELECTOR, '.player-link').get_attribute('href')
driver.get(author_name)
username = driver.find_element(By.CSS_SELECTOR, '.h1.flex-grow-1.font-weight-regular').text
print(username)
link_to_profile = driver.current_url
driver.back()
print(link_to_profile)
social_media = driver.find_element(
By.XPATH,
'.//*[@id="app"]/div/main/div/div/div/div[1]/div[1]/dl/dd[5]/a/span'
).text.lower()
print(social_media)
except:
username = ""
link_to_profile = ""
social_media = ""
if re.search(r'[а-яА-ЯёЁіІїЇєЄґҐ]', username):
print(f"Processing map: {map_name}")
ws[f'A{excel_index}'] = username
ws[f'F{excel_index}'] = map_url
ws[f'L{excel_index}'] = link_to_profile
ws[f'R{excel_index}'] = social_media
excel_index += 1
driver.get(url)
time.sleep(5)
# try:
# next_page_button = wait.until(EC.element_to_be_clickable(
# (By.CSS_SELECTOR, '.ml-1.v-btn.v-btn--is-elevated.v-btn--has-bg.v-btn--tile.theme--dark.v-size--large.blue.darken-4')
# ))
# next_page_button.click()
# time.sleep(3)
# url = driver.current_url
# driver.get(url)
# except:
# print("No more pages to process.")
# break
break
finally:
work_book.save(file)
work_book.close()
driver.quit()
print(map_creators)
# try:
# for map_creator in map_creators:
# map_creator.add_map_creator()
# work_book.save(file)
# except Exception as exception:
# print(exception)
# finally:
# work_book.close()