Python没有权限访问此服务器/从ZIP返回城市/州 [英] Python Doesn't Have Permission To Access On This Server / Return City/State from ZIP

查看:85
本文介绍了Python没有权限访问此服务器/从ZIP返回城市/州的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要尝试的是从邮政编码中检索城市和州.这是我到目前为止的内容:

What I'm trying to do is retrieve the city and state from a zip code. Here's what I have so far:

def find_city(zip_code):
    zip_code = str(zip_code)
    url = 'http://www.unitedstateszipcodes.org/' + zip_code
    source_code = requests.get(url)
    plain_text = source_code.text
    index = plain_text.find(">")
    soup = BeautifulSoup(plain_text, "lxml")
    stuff = soup.findAll('div', {'class': 'col-xs-12 col-sm-6 col-md-12'})

我也尝试使用id ="zip-links",但这没有用.但这就是问题:运行print(plain_text)时,我得到以下信息:

I also tried using id="zip-links", but that didn't work. But here's the thing: when I run print(plain_text) I get the following:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /80123
on this server.<br />
</p>
</body></html>

所以我想我的问题是这样:是否有更好的方法从邮政编码获取城市和州?还是有一个原因unitedstateszipcodes.gov不合作.毕竟,很容易看到源,标签和文本.谢谢

So I guess my question is this: is there a better way to get a city and state from a zip code? Or is there a reason that unitedstateszipcodes.gov isn't cooperating. After all, it is easy enough to see the source and tags and text. Thank you

推荐答案

我认为您需要花更长的时间来解决一个简单的问题!

I think you are taking a longer route to solve an easy problem!

尝试 pyzipcode

>>> from pyzipcode import ZipCodeDatabase
>>> zcdb = ZipCodeDatabase()
>>> zipcode = zcdb[54115]
>>> zipcode.zip
u'54115'
>>> zipcode.city
u'De Pere'
>>> zipcode.state
u'WI'
>>> zipcode.longitude
-88.078959999999995
>>> zipcode.latitude
44.42042
>>> zipcode.timezone
-6

这篇关于Python没有权限访问此服务器/从ZIP返回城市/州的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆