更新异常处理情况
@time 2013-0803 更新循环里计数问题和随机等待时间问题
#!/usr/bin/python
#-*- coding:utf-8 -*-
‘‘‘
此脚本主要实现网页的点击量,除了实现次功能点外,还有三个知识点:
1、随机获取代理ip,通过代理ip访问指定站点,其目的是防止ip被封
2、访问一个页面后,随机休息几秒,再访问,其目的是防止网站前面有4-7层过滤设备拦截
3、修改http的user agent字段,有些网站和4-7层设备会检查
Created on 2013-7-14
@author: QQ136354553
‘‘‘
import urllib2,re,time,urllib,proxyIP,random,user_agents
def getHtml(url):
proxy_ip =random.choice(proxyIP.proxy_list) #在proxy_list中随机取一个ip
print proxy_ip
proxy_support = urllib2.ProxyHandler(proxy_ip)
opener = urllib2.build_opener(proxy_support,urllib2.HTTPHandler)
urllib2.install_opener(opener)
request = urllib2.Request(url)
user_agent = random.choice(user_agents.user_agents) #在user_agents中随机取一个做user_agent
request.add_header(‘User-Agent‘,user_agent) #修改user-Agent字段
print user_agent
html = urllib2.urlopen(request).read()
return proxy_ip
urls =
[‘http://www.25shiyan.com/?fromuid=16‘,‘http://www.25shiyan.com/forum.php?mod=viewthread&tid=37840&extra=page%3D1‘,‘http://www.25shiyan.com/forum.php?mod=viewthread&tid=36786&extra=page%3D1‘]
count_True,count_False,count= 0,0,0
while True:
for url in urls:
count +=1
try:
proxy_ip=getHtml(url)
except urllib2.URLError:
print ‘URLError! The bad proxy is %s‘ %proxy_ip
count_False += 1
except urllib2.HTTPError:
print ‘HTTPError! The bad proxy is %s‘ %proxy_ip
count_False += 1
except:
print ‘Unknown Errors! The bad proxy is %s ‘ %proxy_ip
count_False += 1
randomTime = random.uniform(1,3) #取1-10之间的随机浮点数
time.sleep(randomTime) #随机等待时间
print ‘%d Eroors,%d ok,总数 %d‘ %(count_False,count - count_False,count)
######################
上面引入模块中:proxyIP、user_agents内容如下:
######################
proxyIP.py
#!/usr/bin/python
#-*- conding:utf-8 -*-
proxy_list = [
{‘http‘:"http://59.53.67.215:80"},
{‘http‘:"http://60.161.14.77:8001"},
{‘http‘:"http://61.144.14.68:80"},
{‘http‘:"http://61.144.68.180:9999"},
{‘http‘:"http://61.164.108.84:8844"},
{‘http‘:"http://61.166.55.153:11808"}
]
###########################
user_agents.py
#!/usr/bin/python
#-*- coding:utf-8 -*-
import random
user_agents = [
‘Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11‘,
‘Opera/9.25 (Windows NT 5.1; U; en)‘,
‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)‘,
‘Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)‘,
‘Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12‘,
‘Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9‘
]
####################################
1、代理ip目前只是静态列表,想做成动态获取的,目前还没实现,后续考虑
2、urls没有处理好,最初是想从一个主战点,抓取子链接的,现在还没有实现