python通过代理刷网页点击量
2013-07-15 22:08
274 查看
#!/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 html
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 = 0
while count < 10000:
for url in urls:
getHtml(url)
count += 1
randomTime = random.uniform(1,10) #取1-10之间的随机浮点数
time.sleep(randomTime) #随机等待时间
print 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没有处理好,最初是想从一个主战点,抓去子链接的,现在还没有实现
有很多地方没有想好怎么处理,如果有好的建议请赐教~
#-*- 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 html
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 = 0
while count < 10000:
for url in urls:
getHtml(url)
count += 1
randomTime = random.uniform(1,10) #取1-10之间的随机浮点数
time.sleep(randomTime) #随机等待时间
print 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没有处理好,最初是想从一个主战点,抓去子链接的,现在还没有实现
有很多地方没有想好怎么处理,如果有好的建议请赐教~
相关文章推荐
- python通过代理刷网页点击量
- python通过代理刷网页点击量
- python通过代理刷网页点击量
- 通过代理刷网页点击量
- python通过xlwt模块直接在网页上生成excel文件并下载
- Python 通过ISA 代理访问互联网报错 HTTP Error 407
- python网页爬虫代理
- 代理刷网页点击量-点到为止 2014-03-11 09:40
- 通过python和websocket捕获进程的输出到网页上
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- [python]通过urllib2设置代理访问网址
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- python通过urllib2爬网页上种子下载示例
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
- C# webBrowser 通过代理访问网页
- python爬虫学习(一)通过urllib2模块获取html,设置用户代理
- 微信网页开发通过Fiddler代理实现测试微信支付功能
- Python通过解析网页实现看报程序的方法
- Python开发中爬虫使用代理proxy抓取网页的方法示例
- [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容