I'm trying to build a dataframe based on web scraping of that page
https://www.schoolholidayseurope.eu/choose-a-country
html firstable i said to selenium to click on page of my choice then i put xpath and tags elements for build header and body but i don't have the format that i desired my element is NaN or duplicates.
Following my script :
def get_browser(url_selector):
"""Get the browser (a "driver")."""
#option = webdriver.ChromeOptions()
#option.add_argument(' — incognito')
path_to_chromedriver = r"C:/Users/xxxxx/Downloads/chromedriver_win32/chromedriver.exe"
browser = webdriver.Chrome(executable_path= path_to_chromedriver)
browser.get(url_selector)
""" Try with Italie"""
browser.find_element_by_xpath(italie_buton_xpath).click()
""" Raise exception : down browser if loading take more than 45sec : timer is the logo website as a flag"""
# Wait 45 seconds for page to load
timeout = 45
try:
WebDriverWait(browser, timeout).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="s5_logo_wrap"]/img')))
except TimeoutException:
print("Timed out waiting for page to load")
browser.quit()
return browser
browser = get_browser(url_selector)
headers = browser.find_element_by_xpath('//*[@id="s5_component_wrap_inner"]/main/div[2]/div[2]/div[3]/table/thead').find_elements_by_tag_name('tr')
headings = [i.text.strip() for i in headers]
bs_obj = BeautifulSoup(browser.page_source, 'html.parser')
rows = bs_obj.find_all('table')[0].find('tbody').find_all('tr')[1:]
table = []
for row in rows :
line = next(td.get_text() for td in row.find_all("td"))
print(line)
table.append(line)
browser.quit()
pd.DataFrame(line, columns = headings)
it returns
a one column dataframe like :
School Holiday Region Start date End date Week
0 Easter holidays 2018
1 REMARK: Small differences by region are possi...
2 Summer holiday 2018
3 REMARK: First region through to last region.
4 Christmas holiday 2018
there's three issue there i don't want REMARK rows and school holiday start-date and end-date are taken as separated word and the whole dataframe is unsplitted.
If i split my headings and line the shape of both mismatch due to REMARKS rows i got 9 elements in my list instead of 3 and due to separated words i got 8 elements instead of 5 in heading.