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. Linux内核分析作业 NO.6

    进程的描述和进程的创建 于佳心  原创作品转载请注明出处  <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-100002900 ...

  2. github学习心得

    1.在GitHub上建立项目登录GitHub后,你可以在右边靠中那里找到一个按钮“New Repository”,点击过后,填入项目名称.说明和网址过后就可以创建.2.配置Git以及上传代码安装Git ...

  3. Ajax的注意事项

    case 1: 无论是使用原生的JavaScript,还是JQuery,通过Ajax请求后端程序数据,返回的数据默认是字符串,字符串,字符串,重要的事情说三遍!!! case 2: 不要尝试直接将返回 ...

  4. JavaScript 字符串与数组互转,并保持数据去重、排序功能

    var valueArr = new Array(); if( $("input[name='type']").val() != ""){ valueArr = ...

  5. [转帖]nginx upstream模块--负载均衡

    nginx upstream模块--负载均衡 https://www.cnblogs.com/linjiqin/p/5494783.html Module ngx_http_upstream_modu ...

  6. Oracle 和 SQLSERVER 重新获取统计信息的方法

    1. Oracle 重新获取统计信息的命令 exec dbms_stats.gather_schema_stats(ownname =>) # 需要修改 ownername options 指定 ...

  7. 【转】fscanf 跳过空格,读取一行

    fscanf(fp, "%s", sLineWord); 以上语句,在读取一行数据时,如何遇到该行数据有空格,那么读到空格处就停止,不再继续向下读. 若想遇到空格继续读取,读取完整 ...

  8. Django-website 程序案例系列-18 多表跨表操作优化

    详解Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化 在数据库有外键的时候,使用 select_related() 和 pref ...

  9. BZOJ4864[BeiJing 2017 Wc]神秘物质——非旋转treap

    题目描述 21ZZ 年,冬. 小诚退休以后, 不知为何重新燃起了对物理学的兴趣. 他从研究所借了些实验仪器,整天研究各种微观粒子.这 一天, 小诚刚从研究所得到了一块奇异的陨石样本, 便迫不及待地开始 ...

  10. BZOJ4383 Pustynia(线段树+拓扑排序)

    线段树优化建图暴力拓扑排序即可.对于已确定的数,拓扑排序时dp,每个节点都尽量取最大值,如果仍与已确定值矛盾则无解.叶子连出的边表示大于号,其余边表示大于等于. #include<iostrea ...