前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python分析ip地理位置

python分析ip地理位置

作者头像
py3study
发布2020-01-09 17:09:23
1.3K0
发布2020-01-09 17:09:23
举报
文章被收录于专栏:python3python3
#!/usr/bin/env python
 #coding=utf8
 
 ################################
 # 通过ip138判断ip所在地区
 #               第一版
 #                author:xxxx
 ##################################
 
 import sys
 import urllib2
 import urllib
 import httplib
 import re
 
 '''
 ip 查询格式
 http://www.ip138.com/ips8.asp?ip=112.254.67.142&action=2'''
 
 #url = "http://www.ip138.com/ips8.asp?ip="
 #ip = "112.254.67.142"
 #mark = "&action=2"
 #getString = url + ip + mark
 
 #rep = urllib2.Request(getString)
 #fd = urllib2.urlopen(rep)
 #while 1:
      #data = fd.read(1024)
      #if not len(data):
           #break
      #print data
 
 web_url = "www.ip138.com"     
 u1 = "/ips8.asp?ip="
 u2 = "112.254.67.142"
 u3 = "&action=2"
 #getUrl = u1 + u2 + u3
 
 fd = open('1.txt','r+')
 for line in fd.readlines():
     #rstrip去掉右边空格和回车换行符
     getUrl = u1 + line.rstrip() + u3
 
     conn = httplib.HTTPConnection(web_url)
     conn.request("GET", getUrl)
     r1 = conn.getresponse()
     data1 = r1.read()
     #都是将gb2312编码的str转为unicode编码
     #print data1.decode('gb2312')
     #keyword = re.compile(r'''<td align="center"><ul class="ul1"><li>(.*?)</li></ul></td>''', re.U|re.S)
     keyword = re.compile(r'''<td align="center"><ul class="ul1"><li>(.*?)</li><li>''', re.U|re.S)
     a = re.findall(keyword,data1)
     print line.strip() + "\t" + a[0].decode('gb2312')
     conn.close()
 
 fd.close()

1.txt 内容如下:

119.128.200.90 218.90.169.90 219.129.242.180 183.69.189.172 222.89.193.18 221.193.222.105 59.151.213.135 218.22.182.50 124.238.192.23 113.111.94.116 115.182.53.30

执行上面的程序就会显示ip的地理位置。需要python环境支持。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档