from faker import Faker
import requests
# 随机ip
faker = Faker('zh-CN')
ip = faker.ipv4()
def search_ip_location(ip):
url = f'https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={ip}&resource_id=6006'
req = requests.get(url=url)
body = req.json()
mes = body.get('data')[0]
result = {'location': mes['location'], 'origip': mes['origip']}
return result # {'location': '日本', 'origip': '192.31.197.16'}
网友评论