python 根据ip定位省市

import requests
requests.adapters.DEFAULT_RETRIES = 10
from bs4 import BeautifulSoup


def get_address(ip):
    url = "http://ip138.com/ips138.asp"
    kw2 = {'ip': ip}
    r = requests.request('GET', url, params=kw2)
    r.encoding = 'gbk'
    demo = r.text
    soup = BeautifulSoup(demo, "html.parser")
    soup = soup.ul
    address = soup.contents[0].string[5:]
    address = address.split(' ')[0]
    return address

你可能感兴趣的:(python)