import requests
from bs4 import BeautifulSoup
def queryIpAddress(ipaddress):
# reaponse = requests.get("https://ip.cn/index.php?ip={}".format(ipaddress))
# soup = BeautifulSoup(reaponse.content, 'lxml')
# a = soup.find('div', attrs={'class': 'well'})
# address = a.find_all('code')[1].text
# 以上4行代码是获取中文的地理位置
reaponse = requests.get("https://ip.cn/index.php?ip={}".format(ipaddress), timeout=10)
soup = BeautifulSoup(reaponse.content, 'lxml')
a = soup.find('div', attrs={'class': 'well'})
b = a.find_all('p')[3].text
address = b.replace("GeoIP: ", "")
# 以上返回 英文或拼音的地理位置
return address
本文介绍了一个使用Python的requests和BeautifulSoup库查询IP地址地理位置的方法。通过访问ip.cn网站,可以获取到IP地址对应的英文或拼音地理位置信息。
1449

被折叠的 条评论
为什么被折叠?



