[Tutor] Beautiful Soup

Crusier crusier at gmail.com
Tue Oct 13 00:21:23 EDT 2015


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)


More information about the Tutor mailing list