Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to for-while loop using function variable values?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading