[Tutor] Problem on handling if statement

Crusier crusier at gmail.com
Wed Nov 25 04:28:24 EST 2015


Dear All,

I am trying to do some web scraping. Attached below is my code:


from bs4 import BeautifulSoup
import requests

#string = str(0175, 0005, 1177)
url = "https://www.etnet.com.hk/www/eng/stocks/realtime/quote.php?code=0175"


def web_scraper(url):
    response = requests.get(url)
    html = response.content
    soup = BeautifulSoup(html,"html.parser")

    real_time_down = soup.find("span", attrs = {"class": "Price down2"})
    real_time_up = soup.find("span", attrs = {"class": "Price up2"})
    real_time_unchange =  soup.find("span",attrs = {"class" :"Price unchange2"})
    change_percent = soup.find("span", attrs = {"class" :"Change"})

    if real_time_down == soup.find("span", attrs = {"class" : "Price
down2"}) or real_time_up == soup.find("span", attrs \
    = {"class": "Price up2"}) or real_time_unchange ==
soup.find("span",{"class" : "Price unchange2"}) :
       print(real_time_down)
       print(real_time_up)
       print(real_time_unchange)
       print(change_percent.string)

    else:
        return None

web_scraper(url)

I have problem trying to get rid of None object. For example, if I put
in 1177 to the url, the real_price_down and the real_time_unchange
will become a None Object. I hope that the program will be able to
sort it out automatically and able to print out the string.


Please help. Thank you very much

Regards,
Henry


More information about the Tutor mailing list