Update - Solved
I have solved this as I realized I need a len function, not index function (and also was using int). Solution was:
address = addressChunk[0] if len(addressChunk) == 3 else '-'
addresses.append(address)
Original Text
I am scraping a website and the business address generally follows [Address, Province Code, Postal Code], however sometimes the address is entered like [Address Province Code, Postal Code] or some variation that is less than an index of 2 (assuming it goes 0, 1, 2).
I have the following code, to strip the respective values from their index in the list based on commas, and add it to a variable if the list index equals 2, otherwise it should display "-", but I'm getting the below error. Note that the code works on normally formatted addresses when the if statements are removed.
# initiate storage
addresses = []
provinces = []
postalCodes = []
# grab container html with regex to find randomized result id
companies_div = soup.find_all('div', {'id': re.compile('result-id-.*')})
for x in companies_div:
#extract address class and split by comma
addressChunk = x.find('div', class_='result__address').text.split(',')
# assign first index (address) to addresses list if index equals 2
address = addressChunk[0] if int(addressChunk.index) == int(2) else '-'
addresses.append(address)
# same as above but for second index
province = addressChunk[1] if int(addressChunk.index) == int(2) else '-'
provinces.append(province)
postalCode = addressChunk[2] if int(addressChunk.index) == int(2) else '-'
postalCodes.append(postalCode)
However, I end up with the following error:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'builtin_function_or_method'
Note that when I remove the "int" from the formula, it results in even properly formatted addresses showing up as "-".