[Tutor] Beautiful Soup

Cameron Simpson cs at zip.com.au
Tue Oct 13 01:13:32 EDT 2015


On 12Oct2015 21:21, Crusier <crusier at gmail.com> wrote:
>I am using Python 3.4. I am trying to do some web scraping at this moment.
>I got stuck because there is an IndexError: list index out of range if I
>put stock_code = (18). My desire output is that the script is able to
>detect print out the recent price whether it is up, down or unchanged.

Just a remark: you write:

  var = (value)

a lot. It's ok, but needless and also slightly prone to turning into a tuple if 
there's a stray comma. Anyway...

It would be helpful to have the exact error message. You say "there is an 
IndexError: list index out of range", but that can occur in several places in 
your code. Normally Python will print a stack trace indicating the specific 
place in your code where the exception occurred.

Please always include the complete error message when asking for help.

[...]
>        print('Now is trading at UP', item.select('.up2')[0].text)

Everywhere you have [0] you may get an IndexError if the select returns an 
empty list, because there will be no element 0.

>    elif item.select('.down2') == item.select('.down2'):

I'm curious: how can this test ever be false?

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the Tutor mailing list