一、需求背景

网站刷票时,经常会遇到限制一个ip只能投票一次的限制,为此需要使用代理ip

二、脚本如下:

1、Proxy_http.py使用代理ip发送httpr的get和post请求

 #coding:utf-8
 import urllib2,urllib,time,socket,random,Proxy_ip,Useragent

 def Visitpage(proxyip,url):
     socket.setdefaulttimeout(6)
     proxy_support = urllib2.ProxyHandler({'http':proxyip})
     user_agent = random.choice(Useragent.user_agents)
     opener = urllib2.build_opener(proxy_support,urllib2.HTTPHandler)
     urllib2.install_opener(opener)
     try:
         request = urllib2.Request(url)
         request.add_header('Referer','http://www.baidu.com')
         request.add_header('User-Agent',user_agent)
         html = urllib2.urlopen(request).read()
         print html
         time.sleep(random.randint(60,180))
     except urllib2.URLError,e:
         print 'URLError! The bad proxy is %s' %proxyip
     except urllib2.HTTPError,e:
         print 'HTTPError! The bad proxy is %s' %proxyip
     except:
         print 'Unknown Errors! The bad proxy is %s ' %proxyip

 def Clicklikebutton(proxyip,url,data):
     socket.setdefaulttimeout(6)
     proxy_support = urllib2.ProxyHandler({'http':proxyip})
     user_agent = random.choice(Useragent.user_agents)
     opener = urllib2.build_opener(proxy_support,urllib2.HTTPHandler)
     try:
         request = urllib2.Request(url)
         request.add_header('Referer','http://www.baidu.com')
         request.add_header('User-Agent',user_agent)
         data = urllib.urlencode(data)
         resp = opener.open(request, data)
         print resp.read()
         time.sleep(random.randint(60,180))
     except urllib2.URLError,e:
         print 'URLError! The bad proxy is %s' %proxyip
     except urllib2.HTTPError,e:
         print 'HTTPError! The bad proxy is %s' %proxyip
     except:
         print 'Unknown Errors! The bad proxy is %s ' %proxyip

 def main():
     for i in range(len(Proxy_ip.iplist)):
         proxyip = Proxy_ip.iplist[i]
         i += 1
         print proxyip
         for m in range(random.randint(2,4)):
                      Clicklikebutton(proxyip,'你的post请求地址',{你的post请求参数})

 if __name__ == "__main__":
     main()

2、Useragent.py文件为agent库

#!/usr/bin/python
#-*- coding:utf-8 -*-

user_agents = [
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60'
    'Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.50'
    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.50'
    'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0'
    'Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10'
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2'
    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36'
    'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'
    'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16'
    'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50'
    'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50'
    'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0'
    'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)'
    'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36'
]

3、Proxy_ip.py为读取代理ip文件

#coding:utf-8

iplist = []
datafile = file( "E:\\monkey_test\\http\\1222-1.txt", "r" )

for line in datafile.readlines():
    line = line.strip('\n')
    iplist.append(line)

4、代理ip的获取

可在以下网站获取http://www.xicidaili.com/(可自行开发脚本爬取网站的ip,容易被封,脚本在此不提供)

也可以购买http://www.daxiangdaili.com/

python使用代理ip发送http请求的更多相关文章

  1. python检验代理ip是否可用、代理ip检验

    python检验代理ip是否可用.代理ip检验 安装相关模块: pip install requests 验证代理IP是否可用脚本: import random import telnetlib im ...

  2. C#使用代理IP发送请求

    https://www.cnblogs.com/benbenfishfish/p/5830149.html   获取可代理的IP https://www.cnblogs.com/ShalenChe/p ...

  3. [python]爬代理ip v2.0(未完待续)

    爬代理ip 所有的代码都放到了我的github上面, HTTP代理常识 HTTP代理按匿名度可分为透明代理.匿名代理和高度匿名代理. 特别感谢:勤奋的小孩 在评论中指出我文章中的错误. REMOTE_ ...

  4. Python爬虫代理IP池

    目录[-] 1.问题 2.代理池设计 3.代码模块 4.安装 5.使用 6.最后 在公司做分布式深网爬虫,搭建了一套稳定的代理池服务,为上千个爬虫提供有效的代理,保证各个爬虫拿到的都是对应网站有效的代 ...

  5. python使用代理ip

    python使用代理的方法有两种 1. #先创建代理ip对象 proxy_support = urllib.request.ProxyHandler({'https':'117.64.149.137: ...

  6. python测试代理IP地址

    代码: # -*- coding: utf-8 -*- import urllib,urllib2,re from random import choice from scrapy.selector ...

  7. Python测试代理ip是否有效

    方式一: 通过icanhazip.com返回的ip地址进行检测 import requests '''代理IP地址(高匿)''' proxy = { 'http': 'http://117.85.10 ...

  8. nodejs通过代理(proxy)发送http请求(request)

    有可能有这样的需求,需要node作为web服务器通过另外一台http/https代理服务器发http或者https请求,废话不多说直接上代码大家都懂的: var http = require('htt ...

  9. python获取代理IP

    利用requests库获取代理,用Beautiful库解析网页筛选ip # -*- coding: utf- -*- import requests from bs4 import Beautiful ...

随机推荐

  1. mysql密码重置

    忘记了Mysql的root用户密码,尝试连接时提示: mysqladmin: connect to server at 'localhost' failed ERROR : Access denied ...

  2. union和union all的区别

    UNION 写一篇联合查询(把前后两个表的查询结果集合在前表中)首先有个为什么需要 相同记录数?? 记错了.应该是union两张表的查询字段数目要一致,字段类型要相似相同的数据类型,至少是相似,可转化 ...

  3. OAF_开发系列19_实现OAF对话框提示dialogPage(案例)

    20150716 Created By BaoXinjian

  4. 安装SQL提示重启电脑失败,解决办法

    1. 打开注册表, 找到HKEY_LOCAL_MACHINE-->software-->Microsof-->MSSQLServer...统统删掉 2.HKEY_LOCAL_MACH ...

  5. mount不是很熟悉 转载文章了解下 转自http://forum.ubuntu.org.cn/viewtopic.php?f=120&t=257333

    纯粹针对刚刚解封开包的新新手,老鸟们请自觉绕行,否则浪费你的时间你非要逼我做谋杀犯可不光我的事你还没地方说理去.如果你正好是个崭新的新手,就耐心的花点时间看看吧,至少大概看看,不要在一个陌生又黑暗的到 ...

  6. linux配置ftp

    参考: http://www.cnblogs.com/acpp/archive/2010/02/08/1665876.html http://blog.csdn.net/huzhenwei/artic ...

  7. LAN-LAN FREE-SERVER FREE

    注:本文转自http://taotao1240.blog.51cto.com/731446/839702 CU上有个板块叫“存储备份”,可见存储和备份是紧紧联系的.在看备份的时候,经常能看到LAN f ...

  8. Windows下python virtualenv使用,镜像源设置,批量安装,安装scipy,numpy

    镜像源设置 在C:\Users\Administrator\下建立pip文件夹,然后在里面创建了一个pip.ini 内容为: [global]index-url = https://pypi.tuna ...

  9. (转)设计模式_Singleton单例模式

    静态初始化 public sealed class Singleton { private static readonly Singleton instance = new Singleton(); ...

  10. Druid 基础使用-操作篇(Pivot、plyql)

    一.Pivot  --9095 端口  二.基本sql 使用 .plysql--   http://plywood.imply.io/plyql  (下面的端口应该是8082,我这个地方做了端口转换) ...