Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门

https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6EmUbbW&id=564564604865

import requests
import time
import sys
import os.path "List of free ip services"
"http://icanhazip.com/"
"http://checkip.dyndns.org/"
"https://freegeoip.net/json/"
"http://www.telize.com/ip"
"http://ip-api.com/json"
"http://curlmyip.com/"
"http://ipinfo.io/ip" #Global variables
ipFile="/tmp/ip.log"
timeout = 10 class Service:
url=""
def request(self): return requests.get(self.url, timeout = timeout) class Icanhazip(Service):
name="icanhazip"
url="http://icanhazip.com/"
def ip(self): return self.request().text.strip() class Freegeoip(Service):
name="freegeoip"
url="https://freegeoip.net/json/"
def ip(self): return self.request().json()["ip"] class Telize(Service):
name="telize"
url="http://www.telize.com/ip"
def ip(self): return self.request().text.strip() class IpApi(Service):
name="ip-api"
url="http://ip-api.com/json"
def ip(self): return self.request().json()["query"] class Ifconfig(Service):
name="ifconfig.me"
url="http://ifconfig.me/all.json"
def ip(self): return self.request().json()["ip_addr"] def request_ip():
#List of services
services = [Icanhazip(), Freegeoip(), Telize(), IpApi(), Ifconfig() ]
for i in range(len(services)): service = services[i]
try:
start = time.time()
print "* Requesting current ip with '{}'".format(service.name)
ip = service.ip()
print "* Request took {} seconds ".format(int(time.time() - start))
return ip
except Exception as error:
print "* Exception when requesting ip using '{}': {} ".format(service.name, error ) error = "Non available services, add more services or increase the timeout (services = {}, timeout = {}) ".format(len(services), timeout)
raise RuntimeError(error) def current_ip():
return open(ipFile,"r").readlines()[0] def save_ip(ip):
f = open(ipFile,'w')
f.write(str(ip)) #Main
if os.path.isfile(ipFile) : #File exists
request_ip = request_ip()
current_ip = current_ip() if request_ip != current_ip:
save_ip(request_ip)
print "* IP has changed from {} to {}".format(current_ip, request_ip)
sys.exit(1)
else :
print "* IP is still the same: {}".format(current_ip) else:
request_ip = request_ip()
save_ip(request_ip)
print "* This is the first time to run the ip_change script, I will create a file in {} to store your current address: {} ".format(ipFile, request_ip) #Test
"""
services = [Icanhazip(), Freegeoip(), Telize(), IpApi(), Ifconfig() ]
for i in range(len(services)):
service = services[i]
print "{} ip result: {} ".format(service.name, service.ip() )
"""

  

ip更换的更多相关文章

  1. 记一次服务器迁移 TFS客户端ip更换

    服务器迁移,TFS服务端IP由原10.58.8.231更换至10.58.1.230 TFS客户端更换ip操作比较复杂,请谨慎操作,避免脱库的风险!!! 打开注册表,运行->regedit 找到H ...

  2. k8s集权IP更换

    -.背景描述 背景:在场内进行部署完成后标准版产品,打包服务器到客户现场后服务不能正常使用,因为客户现场的IP地址不能再使用场内的IP,导致部署完的产品环境在客户现场无法使用:此方案就是针对这一问题撰 ...

  3. Linux下检测IP地址冲突及解决方法

    问题说明:在公司办公网内的一台物理机A上安装了linux系统(ip:192.168.9.120),在上面部署了jenkins,redmine,svn程序.由于是在办公网内,这台机器和同事电脑都是在同一 ...

  4. selenium在scrapy中的使用、UA池、IP池的构建

    selenium在scrapy中的使用流程 重写爬虫文件的构造方法__init__,在该方法中使用selenium实例化一个浏览器对象(因为浏览器对象只需要被实例化一次). 重写爬虫文件的closed ...

  5. Lvs IP负载均衡技术

    Lvs集群的通用结构 Lvs集群采用IP负载均衡技术,属于IP层的交换(L4),具有很好的吞吐率.调度器分析客户端到服务器的IP报头信息,将请求均衡地转移到不同的服务器上执行,且调度器自动屏蔽掉服务器 ...

  6. 腾讯云API弹性公网IP踩坑

    由于自己管理的云服务器数量比较多,时不时需要更换IP,在管理台上一下下点击,实在浪费时间,于是就想到了通过API调用的方式,将更换IP一系列动作,全部集成到Python代码里面,实现一行命令,完成IP ...

  7. 中间件使用之(UA,IP,selenium)的使用

    一.UA池:User-Agent池 - 作用:尽可能多的将scrapy工程中的请求伪装成不同类型的浏览器身份. - 操作流程: 1.在下载中间件中拦截请求 2.将拦截到的请求的请求头信息中的UA进行篡 ...

  8. 网络爬虫一定要用代理IP吗

    数据采集现在已经成为大数据时代不可以缺少的一部分,在数据采集过程中,很多人都会用到代理ip,那么网络爬虫一定要用代理IP吗?答案虽然不是肯定的,但出现以下情况一定是需要用到代理IP的.1.在爬虫的时候 ...

  9. 探索ASP.NET MVC5系列之~~~3.视图篇(下)---包含常用表单和暴力解猜防御

    其实任何资料里面的任何知识点都无所谓,都是不重要的,重要的是学习方法,自行摸索的过程(不妥之处欢迎指正) 汇总:http://www.cnblogs.com/dunitian/p/4822808.ht ...

随机推荐

  1. Java面向对象(Eclipse高级、类与接口作为参数返回值)

      面向对象 今日内容介绍 u Eclipse常用快捷键操作 u Eclipse文档注释导出帮助文档 u Eclipse项目的jar包导出与使用jar包 u 不同修饰符混合使用细节 u 辨析何时定义变 ...

  2. IP工具类

    import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletReques ...

  3. JQuery Cross Domain Ajax(jsonp)

    http://www.pureexample.com/jquery/cross-domain-ajax.html http://www.pureexample.com/ExampleTesterII- ...

  4. 组件vue传值

    <div id="app"> <hs :message="name"></hs> 用来接收值 </div> &l ...

  5. php 文件上传 $_FILES 错误码

    假设文件上传字段的名称file_name,则: $_FILES['file_name']['error']有以下几种类型 1.UPLOAD_ERR_OK 其值为 0,没有错误发生,文件上传成功. 2. ...

  6. Win10 打开 ubuntu子系统

    1. 修改windows的设置, 增加开发人员模式 针对开人员模式 使用添加删除程序 添加 ubuntu子系统的角色 运行输入control.. 然后添加删除程序 安装完后重启 运行输入 bash 就 ...

  7. 日常工作: 应用服务器Oracle驱动问题说明

    1. .net使用Oracle驱动的发展等 作为.net线的产品的ERP产品 最初的版本 使用了.net 1.1发布时 微软提供的Oracle的驱动 但是后续微软的.net产品线貌似没有继续发展相应的 ...

  8. centos7黑客帝国装逼

    黑客帝国既视感 搜 cmatrix 然后放到本地解压缩 ,安装 yum install ncurses-devel./configure && make && make ...

  9. redis map存储的注意点

  10. BZOJ4723[POI2017]Flappy Bird——模拟

    题目描述 <飞扬的小鸟>是一款风靡的小游戏.在游戏中,小鸟一开始位于(0,0)处,它的目标是飞到横坐标为X的某个位置 上.每一秒,你可以选择点击屏幕,那么小鸟会从(x,y)飞到(x+1,y ...