I am trying to scrape an address. The below is my try:
city_list = soup.select('div.sc-183mtny-0.IconRow___StyledBox-sc-1f6s35j-0.jqaQXH.ilrzEg span.mwxddt-0.IconRow___StyledText-sc-1f6s35j-1.hgfkgN.bkjkrD') if soup.select('div.sc-183mtny-0.IconRow___StyledBox-sc-1f6s35j-0.jqaQXH.ilrzEg span.mwxddt-0.IconRow___StyledText-sc-1f6s35j-1.hgfkgN.bkjkrD') else ""
city = city_list[2].text if city_list[2] else ""
The city name(address) is in the third span within the div I have mentioned in the city_list. First I get a list of all the spans within the div and I tried in getting the third element from it. In some web pages it does not exist. Hence, I have used the if else method to check to check the existence and return "", if not.
It runs for some cases but breaks with the following error.
IndexError: string index out of range
I know it says that the city_list[2] does not exists. But I have added an else method, why does not it works?
CodePudding user response:
Else statement does not work like that it only executes if the condition of if statement is wrong. Here your condition is not going wrong instead it is throwing an error. So adding a try - catch block would be suitable for this scenario.
CodePudding user response:
The condition is raising error because the list does not have enough item, So its better to check the length of the list,
city = city_list[2].text if len(city_list) > 2 else ""
Or you can use try-except
block like this,
try:
city = city_list[2].text
except IndexError:
city = ""