直接上代码吧
1 # -*- coding:utf-8 -*- 2 3 import urllib2 4 import random 5 6 url = "http://www.baidu.com/" 7 8 # 可以是User-Agent列表,也可以是代理列表 9 ua_list = [ 10 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv2.0.1) Gecko/20100101 Firefox/4.0.1", 11 "Mozilla/5.0 (Windows NT 6.1; rv2.0.1) Gecko/20100101 Firefox/4.0.1", 12 "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11", 13 "Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11", 14 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11" 15 ] 16 17 # 在User-Agent列表里随机选择一个User-Agent 18 user_agent = random.choice(ua_list) 19 20 # 构造一个请求 21 request = urllib2.Request(url) 22 23 # add_header()方法 添加/修改 一个HTTP报头 24 request.add_header("User-Agent", user_agent) 25 26 # get_header() 获取一个已有的HTTP报头的值,注意只能是第一个字母大写,其他的必须小写 27 print request.get_header("User-agent")
打印出来是:
Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11
[Finished in 1.4s]
时间: 2024-10-28 03:33:01