将批处理IP地址转换为查找位置

时间:2013-12-02 04:10:59

标签: javascript python ip

是否有任何服务可以让我确定1000多个IP地址的位置?我遇到的服务将其限制为20,40,50或100个IP地址。这是可以用Python编程的东西吗?我在JavaScript / Python方面经验很少。不确定是否有API或服务可以让我检索此信息。我听说过GeoIP,但不知道从哪里开始。

谢谢。

5 个答案:

答案 0 :(得分:1)

我刚写了script来处理这件事。

以下是来源:

import csv
import requests


def get_addresses(filename):
    """
    Given a CSV file, this function returns a list of lists
    where each element (list) in the outer list contains the
    row info from the csv file.
    """
    all_addresses = []
    with open(filename, 'rb') as f:
        reader = csv.reader(f)
        for row in reader:
            all_addresses.append(row)
    return all_addresses


def get_geolocation(all_the_ip_address):
    """
    Given a list of lists from `get_addresses()`, this function
    returns an updated lists of lists containing the geolocation.
    """
    print("Getting geo information...")
    updated_addresses = []
    counter = 1
    # update header
    header_row = all_the_ip_address.pop(0)
    header_row.extend(['Country', 'City'])
    # get geolocation
    for line in all_the_ip_address:
        print "Grabbing geo info for row # {0}".format(counter)
        r = requests.get('https://freegeoip.net/json/{0}'.format(line[0]))
        line.extend([str(r.json()['country_name']), str(r.json()['city'])])
        updated_addresses.append(line)
        counter += 1
    updated_addresses.insert(0, header_row)
    return updated_addresses


def create_csv(updated_address_list):
    """
    Given the updated lists of lists from `get_geolocation()`, this function
    creates a new CSV.
    """
    with open('output.csv', 'wb') as f:
        writer = csv.writer(f)
        writer.writerows(updated_address_list)
    print "All done!"


if __name__ == '__main__':
    csv_file = '25_sample_csv.csv'
    all_the_ip_address = get_addresses(csv_file)
    updated_address_list = get_geolocation(all_the_ip_address)
    create_csv(updated_address_list)

希望有所帮助。

答案 1 :(得分:0)

免费数据库:http://dev.maxmind.com/geoip/geoip2/geolite2/

这些API:http://dev.maxmind.com/geoip/geoip2/downloadable/#MaxMind_APIs

使用Python API在Python中编写脚本,该脚本在API中查询每个IP地址,然后记录结果。

答案 2 :(得分:0)

我建议blockfinder获取来自IP注册商的信息。我所看到的每个GeoIP提供商都拥有相同的数据,除非他们得到真实数据仓库的支持(比普通人更贵,想想:'信用卡公司')。

是的,授予IP可能不属于确切的省份,但你至少可以按国家缩小它,这对大多数人来说可能已经足够了。

警告,上次我看到那里只有阻挡命令实用程序 - 我甚至不知道他们是否暴露了API。但是,它是用Python编写的。

答案 3 :(得分:0)

来自ipapi.co的{​​{3}}查找工具可用于将大约一千个IP地址转换为位置。

它可以通过Web界面和可以从Python / Javascript / PHP等调用的API获得。

答案 4 :(得分:0)

您可以使用此免费的在线ip位置批量查找工具。据我所知,没有限制。只需复制并粘贴您的列表即可。可能需要等待一段时间才能通过列表。

https://reallyfreegeoip.org/bulk

相关问题