用Python输入身份证号码前两位输出所在地
时间: 2023-05-12 13:07:09 浏览: 74
可以使用以下代码实现:
```python
import requests
def get_location(id_num):
url = 'http://qq.ip138.com/idsearch/index.asp?action=idcard&userid=' + id_num[:2]
response = requests.get(url)
response.encoding = 'gb2312'
location = response.text.split('<td align="center" bgcolor="#FFFFFF">')[1].split('</td>')[0]
return location
id_num = input('请输入身份证号码:')
location = get_location(id_num)
print('该身份证号码所在地为:', location)
```
注意:此代码仅供学习参考,请勿用于非法用途。
相关问题
用Python的字典输入身份证号码前两位输出所在地
可以使用以下代码实现:
```
id_dict = {
'11': '北京', '12': '天津', '13': '河北', '14': '山西', '15': '内蒙古',
'21': '辽宁', '22': '吉林', '23': '黑龙江', '31': '上海', '32': '江苏',
'33': '浙江', '34': '安徽', '35': '福建', '36': '江西', '37': '山东',
'41': '河南', '42': '湖北', '43': '湖南', '44': '广东', '45': '广西',
'46': '海南', '50': '重庆', '51': '四川', '52': '贵州', '53': '云南',
'54': '西藏', '61': '陕西', '62': '甘肃', '63': '青海', '64': '宁夏',
'65': '新疆', '71': '台湾', '81': '香港', '82': '澳门', '91': '国外'
}
id_num = input("请输入身份证号码前两位:")
if id_num in id_dict:
print("该身份证号码所在地为:", id_dict[id_num])
else:
print("输入有误,请重新输入!")
```
注意:这里只是简单地通过字典来实现,实际上身份证号码的前两位是有规律的,可以通过算法来计算出所在地。
python输入身份证前六位查询归属地
这个问题需要请求第三方的API来实现,以下是一个示例:
```python
import requests
def query_location(id_num):
url = 'http://apis.juhe.cn/idcard/index'
params = {
'cardno': id_num,
'dtype': 'json',
'key': 'your_api_key', # 替换成你自己的API key
}
resp = requests.get(url, params=params)
result = resp.json()
if result['error_code'] == 0:
return result['result']['area']
else:
return None
id_num = input('请输入身份证号前六位:')
location = query_location(id_num)
if location:
print('归属地为:', location)
else:
print('查询失败')
```
注意,这里使用了一个第三方的身份证查询API(聚合数据),需要自己注册并获取API key。另外,由于身份证号是敏感信息,建议使用HTTPS协议发送请求。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)