简体   繁体   中英

Mechanize random User-Agent

I'm trying to get Mechanize to use a random User-Agent every time I initiate opening a URL. Can someone point me in the right direction that I need to take in order to do this? - I've searched everywhere and couldn't find a reference.

Thanks!

This link gives you a sample User-Agent reference. Example code demonstrating it:

from random import choice
user_agents = ['Mozilla/5.0 (X11; U; Linux; i686; en-US; rv:1.6) Gecko Debian/1.6-7','Konqueror/3.0-rc4; (Konqueror/3.0-rc4; i686 Linux;;datecode)','Opera/9.52 (X11; Linux i686; U; en)']
random_user_agent = choice(user_agents)

You can include as many number of user_agents from the above link into the variable user_agents.

And now just put the random_user_agent into Mechanize by adding header in it during initialization.

I had the same problem when I was doing web crawlers, and this is the solution that I used:

class URLOpener():      
    def opener(self,user_agent):
        cj=cookielib.CookieJar()
        #Process Hadlers
        opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        opener.addheaders=[
                        ('User-Agent', user_agent),
                        ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
                        ('Accept-Language', 'en-gb,en;q=0.5'),
                        ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'),
                        ('Keep-Alive', '115'),
                        ('Connection', 'keep-alive'),
                        ('Cache-Control', 'max-age=0'),
                    ]
        return opener

    #Openers with different User-Agents
    def opener_list(self,f_path):
        #f_path is a path to the file that contains browsers
        f=open(f_path, 'r+')
        count=0
        user_agent_list=list()
        for line in f.xreadlines():
            count+=1
            user_agent_list.append(line[:-1])
        openers=[self.opener(user_agent) for user_agent in user_agent_list]
        return openers

also file that I created looked similar to that:

Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
Mozilla/5.0 (Linux; U; Android 2.3; en-us) AppleWebKit/999+ (KHTML, like Gecko) Safari/999.9
Mozilla/5.0 (Linux; U; Android 2.3.5; zh-cn; HTC_IncredibleS_S710e Build/GRJ90) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.3.5; en-us; HTC Vision Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.3.4; fr-fr; HTC Desire Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; T-Mobile myTouch 3G Slide Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.3.3; zh-tw; HTC_Pyramid Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1
Mozilla/5.0 (Linux; U; Android 2.3.3; zh-tw; HTC_Pyramid Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari
Mozilla/5.0 (Linux; U; Android 2.3.3; zh-tw; HTC Pyramid Build/GRI40) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

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