<reponame>n0rel/self
import requests
from bs4 import BeautifulSoup
import time
from selenium import webdriver
import csv

areas = '''Lake of Outrage
Hammerlocke Hills
Giant's Cap
Dusty Bowl
Giant's Mirror
Stony Wilderness
Bridge Field
Motostoke Riverbank
Watchtower Ruins
East Lake Axewell
North Lake Miloch
West Lake Axewell
Axew's Eye
South Lake Miloch
Giant's Seat
Dappled Grove
Rolling Fields'''

driver = webdriver.Chrome()

list_of_areas = areas.split('\n')

for area in list_of_areas:

    url_area = ''.join(area.split(' ')).lower()
    url = rf'https://www.serebii.net/pokearth/galar/{url_area}.shtml#maxraid'
    driver.get(url)
    page_html = driver.page_source

    html = BeautifulSoup(page_html, 'html.parser')

    max_raid_header = html.find('h2', string='Max Raid Battle Dens')

    main = html.find('main').find_all()

    table_list = []

    z = main[-1].find_previous_siblings()

    l = []

    for el in z:
        if el.name == 'table':
            table_list.append(el)

        elif el.name == 'h2':
            break

    for table in table_list:

        common_den_number = 0
        rare_den_number = 0
        url = ''

        print(area)
        for num, tr in enumerate(table.find('tbody').find_all('tr', recursive=False)):
            if tr.find('img') is not None:
                if num == 0:
                    url = 'https://www.serebii.net/' + tr.find('img')['src']

                    tr = tr.find_next_sibling('tr')
                    for num1, link in enumerate(tr.find_all('a')):
                        den_number = link['href'].split('/')[-1].split('.')[0][3::]

                        if num1 == 0:
                            common_den_number = den_number
                        else:
                            rare_den_number = den_number

                        file_name = f'{area}_{common_den_number}_and_{rare_den_number}'
                        response = requests.get(url)
                        if response.status_code == 200:
                            with open(fr"C:\Users\Norel\Desktop\Den Images\{file_name}.jpg", 'wb') as f:
                                f.write(response.content)

driver.close()

'''

den# | rarity | pokemon | sword | shield
----------------------------------------
10   | common | Tyrogue | yes   | yes
10   | common | Sirfetch| yes   | no


'''
