简体   繁体   中英

Why doesn't `if count is 5` work properly?

I am developing an application that has a website and a series of keywords and queries the Google API at what position they are up to page 5.

For example:

Website: `www.stackoverflow.com`
Keywords: `web design`, `programming`, `help`

This will search up to the 5th page on Google and if it's not found then it will create an object with the keyword and the position would be "n/a".

I have created the following recusive function:

def getRankingsFromGoogle(keyword, website, page = 1, count = 1): 

    if count is 5: 
       print "NOT ON PAGE 5" 

    service = build("customsearch", "v1",
              developerKey="")

    res = service.cse().list(
        q=keyword, 
        cx='',
        start=page, 
    ).execute()

    obj = parseGoogleResponse(res, keyword, website)

    if not bool(obj):
        print "adding object to database.."
        return 
    else:
        page = page + 10 
        count += 1
        getRankingsFromGoogle(keyword, website, page, count)


def main(): 

    websites = getTrackedWebsites(); 

    for website in websites: 

        keywords = website['keywords']

        for keyword in keywords: 
            getWebsiteRankingsByKeywords(keyword, website['website'])

The function goes into the else statement, but never actually reaches the if count is 5 which is where I need it to be in order to create the object for the keyword was not found.

I have also created another script:

def getRankingsFromGoogle(keyword, website, page = 1, count = 1):

    if count is 5: 
        print "YAY"
        return;

    page = 10
    count += 1

    print page 
    getRankingsFromGoogle("asf", "www.google.com", page, count)


getRankingsFromGoogle("af", "www.google.com", 1, 1)

Which reaches the condition count is 5 .

I don't understand why this recursive function is not working.

I'm a bit confused on the following logic:

def getRankingsFromGoogle(keyword, website, page = 1, count = 1): 

   if count is 5: 
   print "NOT ON PAGE 5" 

is you're not passing in a count when calling the getRankingsFromGoogle function, why would you ever expect it to hit this condition?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM