python requests更换代理适用于IP频率限制的方法

yipeiwu_com6年前Python基础

有些网址具有IP限制,比如同一个IP一天只能点赞一次。

解决方法就是更换代理IP。

从哪里获得成千上万的IP呢? 百度“http代理” 可获得一大堆网站。

比如某代理网站,1天6元,可以无限提取。

把提取的IP,保存到txt文件中。

写一个方法,读取文件,存入数组中

def getProxysFromFile():
 with open("proxy.txt", "r") as f:
  l = f.readlines()
 return l

比如执行某任务,传入单个代理IP+PORT

def run(proxy):
 
 try:
  print("proxy:{}".format(proxy))
  s=requests.Session()
  proxies={
  "http": "http://{}".format(proxy.strip()), "https":"https://{}".format(proxy.strip())
  }
  header={
   "Host":"www.xxx.com",
   "Referer":"http://www.xxx.com/xxx.html?199",
   "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"
 
  }
  ret=s.get(url="http://www.xxx.com/data/dz?uid=199&ztype=1",headers=header,proxies=proxies,timeout=4)
  rc=ret.content.decode("utf-8")
  print(rc)
  if "成功" in rc:
   global count
   count+=1
   print(count)
 except:
  pass

接下来就是调度,简单写了个调度,比如每隔5秒钟,启动100个线程去执行。(这里为了简单,在上面的run中设置了超时时间为4秒,所以能保证不会导致启动的线程太多未完成卡死)

if __name__ == '__main__':
 count=1
 l=getProxysFromFile()
 while True:
  for i in range(100):
   try:
    t=threading.Thread(target=run,args=(l.pop(),))
    t.start()
   except:
    pass
  time.sleep(5)

效果如下,速度还是很快的。

以上这篇python requests更换代理适用于IP频率限制的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

解决Python3用PIL的ImageFont输出中文乱码的问题

解决Python3用PIL的ImageFont输出中文乱码的问题

今天在用python3+ImageFont输出中文时,结果显示乱码 # coding:utf-8 from PIL import Image, ImageDraw, ImageFon...

Python 实现王者荣耀中的敏感词过滤示例

王者荣耀的火爆就不用说了,但是一局中总会有那么几个挂机的,总能看到有些人在骂人,我们发现,当你输入一些常见的辱骂性词汇时,系统会自动将该词变成“*”,作为python初学者,就想用pyt...

Python3 itchat实现微信定时发送群消息的实例代码

一、简介 1,使用微信,定时往指定的微信群里发送指定信息。 2,需要发送的内容使用excel进行维护,指定要发送的微信群名、时间、内容。 二、py库 1,itchat:这个是主要的工具,...

使用Python实现图像标记点的坐标输出功能

使用Python实现图像标记点的坐标输出功能

Sometimes we have need to interact  with an application,for example by marking points in...

python中利用h5py模块读取h5文件中的主键方法

如下所示: import h5py import numpy as np #HDF5的写入: imgData = np.zeros((2,4)) f = h5py.File('HDF...