Created
July 12, 2012 07:57
-
-
Save shiweifu/3096544 to your computer and use it in GitHub Desktop.
自定义user agent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UA_LIST = [a.split("\n")[0].strip() for a in open("user_agent", "r").readlines()] | |
random_value = lambda lst: random.randint(0, len(lst)) | |
def down_page(url): | |
global UA_LIST | |
headers = { "User-Agent" : random_value(UA_LIST)} | |
req = urllib2.Request(url, headers=headers) | |
response = urllib2.urlopen(req) | |
the_page = response.read() | |
return the_page |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment