Newer
Older
from selenium import webdriver
from selenium.webdriver.common.by import By
def get_cell_in_table(url, table_id):
"""
Use this you can get all the information for the table with specific id
in the page with specific url.
The column and the information will be sac
"""
driver = webdriver.Chrome()
driver.get(url)
model_name = url.split('/')[-1]
table = driver.find_element(by=By.ID, value=table_id)
inline_blocks = table.find_elements(by=By.CLASS_NAME, value="inline-block")
counter = 0
columns = ['model_name']
information = [model_name]
for inline_block in inline_blocks:
table = inline_block.find_element(by=By.TAG_NAME, value="table")
tablerows = table.find_elements(by=By.XPATH, value=".//tr")
for tr in tablerows:
cells = tr.find_elements(by=By.XPATH, value=".//td")
for cell in cells:
cell_text = cell.get_attribute("textContent").strip()
if counter%2 == 0:
columns.append(cell_text)
else:
information.append(cell_text)
counter += 1
driver.quit()
return columns, information
def get_href(url, href_list):
"""
url: The url that you want to get all the possible hyperlinks
href_list: The list where you want to save all the hyperlink
"""
driver = webdriver.Chrome()
driver.get(url)
item_data_div_list = driver.find_elements(By.CSS_SELECTOR, '.list-item .item-data')
href_list = []
for item_data_div in item_data_div_list:
hyperlink = item_data_div.find_element(By.CSS_SELECTOR, 'a.title')
href = hyperlink.get_attribute('href')
href_list.append(href)
driver.quit()
def get_cell_in_charging_table(url, table_class):
"""
Get the cell contect in the table 'Home and Destination Charging' and
'Fast Charging'
Structure
"""
driver = webdriver.Chrome()
driver.get(url)
model_name = url.split('/')[-1]
table = driver.find_element(by=By.CLASS_NAME, value=table_class)
tablerows = table.find_elements(by=By.XPATH, value=".//tr")
with open('charging_table_data.csv', mode='w+') as file:
writer = csv.writer(file)
writer.writerow([table_class])
writer.writerow(['Charging Point', 'Max. Power', 'Power', 'Time Rate'])
for tr in tablerows:
cells = tr.find_elements(by=By.XPATH, value=".//td")
row_data = [cell.get_attribute("textContent").strip() for cell in cells]
writer.writerow(row_data)
driver.quit()