Python 无权访问此服务器/从 ZIP 返回城市/州

Posted

技术标签:

【中文标题】Python 无权访问此服务器/从 ZIP 返回城市/州【英文标题】:Python Doesn't Have Permission To Access On This Server / Return City/State from ZIP 【发布时间】:2016-03-31 18:16:39 【问题描述】:

我要做的是从邮政编码中检索城市和州。这是我目前所拥有的:

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) 时,我得到以下信息:

<!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>

所以我想我的问题是:有没有更好的方法从邮政编码中获取城市和州?或者是否有美国邮政编码.gov 不合作的原因。毕竟,很容易看到源代码、标签和文本。谢谢

【问题讨论】:

不是真正的python问题,但你可以试试邮局:tools.usps.com/go/… 【参考方案1】:

你需要添加一个用户代理:

headers = "User-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36"
def find_city(zip_code):
    zip_code = str(zip_code)
    url = 'http://www.unitedstateszipcodes.org/' + zip_code
    source_code = requests.get(url,headers=headers)

一旦你这样做,响应是 200 并且你得到源:

In [8]:  url = 'http://www.unitedstateszipcodes.org/54115'

In [9]: headers = "User-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36"

In [10]:  url = 'http://www.unitedstateszipcodes.org/54115'
In [11]: source_code = requests.get(url,headers=headers)
In [12]: source_code.status_code
Out[12]: 200

如果您想要详细信息,它很容易解析:

In [59]:  soup = BeautifulSoup(plain_text, "lxml")

In [60]: soup.find('div', id='zip-links').h3.text
Out[60]: 'ZIP Code: 54115'

In [61]: soup.find('div', id='zip-links').h3.next_sibling.strip()
Out[61]: 'De Pere, WI 54115'

In [62]:  url = 'http://www.unitedstateszipcodes.org/90210'

In [63]: source_code = requests.get(url,headers=headers).text

In [64]:  soup = BeautifulSoup(source_code, "lxml")

In [65]: soup.find('div', id='zip-links').h3.text
Out[66]: 'ZIP Code: 90210'

In [70]: soup.find('div', id='zip-links').h3.next_sibling.strip()
Out[70]: 'Beverly Hills, CA 90210'

您还可以将每个结果存储在数据库中,然后首先尝试在数据库中进行查找。

【讨论】:

【参考方案2】:

我认为你在解决一个简单的问题时要走更长的路!

试试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

【讨论】:

我实际上无法让 pyzipcode 工作(有时我的模块无法下载),但我最终使用了您发送的链接上的 .csv 文件。 pyzipcode 是否仅适用于 Linux,因为我只看到了一个 .tar.gz 链接? 使用pip下载模块。 Pip 也失败了 :( 再次,我看到它在下载 x.tar.gz,但我不知道为什么

以上是关于Python 无权访问此服务器/从 ZIP 返回城市/州的主要内容,如果未能解决你的问题,请参考以下文章

Apache:您无权访问此服务器上的 /

在另一台 PC 上出现错误消息“禁止您无权访问 / 在此服务器上”

AWS 用户无权通过显式拒绝访问此资源

您无权访问此服务器上的 /php/send.php 403 Forbidden [重复]

禁止访问:您无权访问此服务器上的/ abcd /

尝试访问 EC2 上的 Web 应用程序时出现“您无权访问 / 在此服务器上”