Discussion:
[Tutor] Beautiful Soup
Crusier
2015-10-13 04:21:23 UTC
Permalink
Hi

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.

Attached is the code:

import requests
from bs4 import BeautifulSoup

stock_code = (939)

url = ("http://www.etnet.com.hk/www/eng/stocks/realtime/quote.php?code=" +
str(stock_code) )
res = requests.get(url).text
soup = BeautifulSoup(res, "html.parser")

for item in soup.select('#StkDetailMainBox'):
if item.select('.up2') == item.select('.up2'):
print('Now is trading at UP', item.select('.up2')[0].text)
elif item.select('.down2') == item.select('.down2'):
print('Now is trading at DOWN', item.select('.down2')[0].text)
elif item.select('.unchange2') == item.select('.unchange2'):
print('Now is trading at UNCHANGE',
item.select('.unchange2')[0].text)

print('Change is ', item.select('.Change')[0].text)


#for item in soup.select('.styleB'):
#print(item.select('.Number')[0].text)
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Cameron Simpson
2015-10-13 05:13:32 UTC
Permalink
Post by Crusier
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.

[...]
Post by Crusier
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.
I'm curious: how can this test ever be false?

Cheers,
Cameron Simpson <***@zip.com.au>
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Loading...