Home > OS >  How to for-while loop using function variable values?
How to for-while loop using function variable values?

Time:01-20

I'm trying to build a for-loop based on function varbiables values. I have this code:

import yagooglesearch

query = "future" #something to google-search

client = yagooglesearch.SearchClient(
query,
start=0,
num=100, #number of result that i want for each loop (100 is the max) 
max_search_result_urls_to_return=500, #total number of urls that i want inside a list
)
client.assign_random_user_agent() #user agent have to change every loop, so put it 
                                  #inside 

urls = client.search() #is the list of urls taken (it returns 100 urls in this case)

I need to get a list of 500 urls, so I need a for or while-loop that when it gets first 100 urls (variable 'num') it changes the 'start' variable to 100, and repeat this operation until it reaches 500 (variable 'max_search_result_urls_to_return') and so create a list with all 500 urls. Any ideas of how to do this? it possible?

CodePudding user response:

iterate in range(5) the search function and use as variable start variable

import yagooglesearch

def query_function(count):
    query = "future" 
    client = yagooglesearch.SearchClient(
    query,
    start=count,
    num=100, 
    max_search_result_urls_to_return=500)
    client.assign_random_user_agent() 
    urls = client.search()
    return urls

all_urls = []
count = 0
for i in range(5):
    urls = query_function(count)
    all_urls.append(urls)
    count  = 100
print(all_urls)
  • Related