Skip to content

Instantly share code, notes, and snippets.

@shiweifu
Created July 12, 2012 07:57
Show Gist options
  • Save shiweifu/3096544 to your computer and use it in GitHub Desktop.
Save shiweifu/3096544 to your computer and use it in GitHub Desktop.
自定义user agent
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