Skip to content
Advertisement

Web scraping: Index out of Bound (Possible scaling error)

Hi Wrote a web scraping program and it gets the ASN number correctly, but after all the data is scraped, it returns a error “Array Out if Bounds”.

I am using Pycharm and latest python version. Below is my code. There is already a similar issue on stackoverflow but I am not able to get the pieces together and make it work. (Web Scraping List Index Out Of Range) its the exact same error but I am not sure how to get it working for my List.

Error seems to be at current_country = link.split(‘/’)[2] Any help is appreciated. Thank you.

JavaScript

Advertisement

Answer

The last href contains string “/countries” in https://ipinfo.io/countries is actually “/countries“:

JavaScript

After splitting this link, it produced list ["", "countries"] where the third element was missing. To fix this problem, simply check the list length before retrieving the third element:

JavaScript

Another solution is to exclude the last href by changing the regexp to:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement