web scraping - Python: import a module but avoid executing it? -


i making scrawler , want randomize request headers.
things goes that:
in configs.py have headers defined:

import random user_agents = ['1', '2', '3']  def get_random_user_agent():     return random.choice(user_agents)  headers = {'user-agent': get_random_user_agent()} 

in main.py have test code that:

from configs import headers in range(5):     print(headers['user-agent']) 

the result same one.
reckon variable 'headers' initialized when importing.
want randomized user agent.
there best practice this? thank you.

that's way python works. code parsed file accessed, code no in function executed immediately. why can have python script this:

import random print(random.randint(1,10)) 

and can executed file python random number.

just define headers in function:

def get_headers():     return {'user-agent': get_random_user_agent()} 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -