'How do I continue a for loop when no data is present which causes an index error?
I am learning python and I'm guessing something very obvious is at fault here. I have a web scraping program that, I suspect, upon finding an empty data field throws an IndexError: list index out of range. I am trying to get the program to ignore this because there is a lot of missing information in what I am scraping. I don't care if my excel sheet has blank or placeholders in the cells either.
I have tried numerous for, if, and elif statements to plow over the missing data.
I figured going as simple as possible might work though to no avail. Any help is appreciated!
import xlsxwriter
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
element_list = []
for page in range(7, 12, 1):
page_url = "https://website.com" + str(page) + "/?geodir_search=1&stype=gd_professional&s=+&snear&sgeo_lat&sgeo_lon"
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get(page_url)
email = driver.find_elements_by_class_name("pg-profile-email")
name = driver.find_elements_by_class_name("pg-profile-name")
address = driver.find_elements_by_class_name("pg-profile-address")
tel = driver.find_elements_by_class_name("pg-profile-tel")
for i in range(len(email)):
element_list.append([email[i].text, name[i].text, address[i].text, tel[i].text])
else:
continue
with xlsxwriter.Workbook('result008.xlsx') as workbook:
worksheet = workbook.add_worksheet()
for row_num, data in enumerate(element_list):
worksheet.write_row(row_num, 0, data)
driver.close()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
