[Tutor] Problem on handling if statement

Laura Creighton lac at openend.se
Wed Nov 25 05:55:35 EST 2015


In a message of Wed, 25 Nov 2015 17:28:24 +0800, Crusier writes:
>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

Why are you returning None in the first place?  Nothing in the code
seems to indicate you need to.

To figure out what is going on, split the code into 3 pieces:

   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",{"class" : "Price unchange2"}) 

Printing them is fine:

   print(real_time_down)
   print(real_time_up)
   print(real_time_unchange)

These 3 lines will print either the value you scraped, or None if it
wasn't found.

   You can say:

   if real_time_down is not None:  # you can also say if real_time_down:
      print(real_time_down)

if you only want things printed if they exist.

But you cannot join them all up the way you did and expect Python to
figure out that you want all the parts that can be printed.

Just write 3 if statements in a row:

     if real_time_down:
     	print(real_time_down)
     if real_time_up:
     	print(real_time_up)
     if real_time_unchanged:
        print(real_time_unchange)

For 3 values, that's not a bad thing.  Reading a screen or more of
them would get hard to read and understand, so ifyou are going to have
a whole lot of these things, then it might be easier to write:

       for data in [real_time_down, real_time_up, 
                    real_time_unchange, <add more here>]:
	  if data:
	     print(data)

HTH,
Laura


More information about the Tutor mailing list