【问题标题】:ip2location for finding ip address of a specific location onlyip2location 仅用于查找特定位置的 IP 地址
【发布时间】:2017-06-21 04:36:50
【问题描述】:

我正在使用 ip2location 在名为 output.txt 的文件中查找 IP 地址列表的位置,并将答案写入另一个文件 ip_info.txt。我只想在我的文件中写入 IP 地址为的条目美国。以下是我的代码。

import IP2Location;
import urllib; 
import time;
start_time = time.time()
IP2LocObj = IP2Location.IP2Location();
IP2LocObj.open("data/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN"); 
 t=open('ip_info','w');
 with open('output','r') as f: # file containing ip addresses
 for line_terminated in f:
     line= line_terminated.rstrip('\r\n'); # strip newline
     if line: # non-blank lines
        rec = IP2LocObj.get_all(line);
        if(rec.country_short == 'US')
           t.write(line);
           t.write('\t');
           t.write(rec.country_short);
           t.write('\n');
print("--- %s seconds ---" % (time.time() - start_time))

我在这里收到以下错误。

File "myprogram.py", line 13
if(rec.country_short == 'US')
SyntaxError: invalid syntax

参考你可以查看https://www.ip2location.com/developers/python

【问题讨论】:

    标签: python ip2location


    【解决方案1】:

    if(rec.country_short == 'US') 不是有效的 Python。

    你的意思是:

    if rec.country_short == 'US':?

    【讨论】:

    • 哦,对不起,我对 python 有点陌生...谢谢 4 d 回答。
    【解决方案2】:

    所有代码

    import IP2Location;
    import urllib; 
    import time;
    start_time = time.time()
    IP2LocObj = IP2Location.IP2Location();
    IP2LocObj.open("data/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN"); 
    t=open('ip_info','w');
    with open('output','r') as f:
         for line_terminated in f:
            line= line_terminated.rstrip('\r\n')
            if line:
              rec = IP2LocObj.get_all(line)
              if rec.country_short == 'US':
                 t.write(line);
                 t.write('\t');
                 t.write(rec.country_short);
                 t.write('\n');
    print("--- %s seconds ---" % (time.time() - start_time))
    

    你的意思是这样的

    【讨论】:

      猜你喜欢
      • 2010-11-20
      • 2018-01-05
      • 2013-11-15
      • 2022-11-05
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      • 2013-05-03
      相关资源
      最近更新 更多