【问题标题】:Python 3: Urllib giving 403 error messagePython 3:Urllib 给出 403 错误消息
【发布时间】:2019-09-27 21:32:14
【问题描述】:

我想运行一个我不久前创建的 python 3 程序,它从某人的特定邮政编码中检索来自网站的天气。几个月前我尝试过它时它运行良好,但现在我收到 urllib 403 错误消息。

我得到了一些建议,有人告诉我该网站不再接受机器人。

我的整个项目是这样的:

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

# asks about zipcode
print("What is your (valid) US zipcode?")

# turns zipcode into a string
zipcode = str(input())

# adds zipcode to the URL
my_url = 'https://weather.com/weather/today/l/' + zipcode + ':4:US'

#Opening up connection, grabbing the page.
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()

# html parsing
page_soup = soup(page_html, "html.parser")

# grabs the temp
weather_data = page_soup.find("div", {"class":"today_nowcard-temp"})

# prints the temp without the extra code
print(weather_data.text)

然后,我被告知在打开连接之前插入这个:

headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0'}

这没有帮助。

我的错误是 403 错误。这是整个消息:

Traceback (most recent call last):
  File "c:/Users/natek/Downloads/Test.py", line 14, in <module>
    uClient = uReq(my_url)
  File "C:\Users\natek\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\natek\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 531, in open
    response = meth(req, response)
  File "C:\Users\natek\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\natek\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 569, in error
    return self._call_chain(*args)
  File "C:\Users\natek\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Users\natek\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

我有点难过,需要一些帮助。我应该完全选择一个新网站吗?

【问题讨论】:

  • 您可以发布您用于设置标题的实际代码吗?您设置了headers 变量,但我们看不到它在哪里使用。

标签: python python-3.x beautifulsoup urllib


【解决方案1】:

根据您的说法,该网站不接受缺少某种身份验证技术的请求。在快速请求记录中,我可以看到正在发出此请求:

https://api.weather.com/v3/location/search?apiKey=d522aa97197fd864d36b418f39ebb323&format=json&language=en-US&locationType=locale&query=[SOMETHING I TYPED]

如果你分解查询字符串,你可以看到apiKey=d522aa97197fd864d36b418f39ebb323。这意味着您需要在请求中提供 API 密钥,它会按预期工作。

我会寻求检查网站是否有方法让您注册和获取 API 密钥,允许您直接发出请求,可能基于一组规则。

我在下面提供了一个使用当前提供的 API 密钥的示例(应该在几个小时内失效,但我会试一试)。

const weatherApi = 'https://api.weather.com/v3/location/search?apiKey=d522aa97197fd864d36b418f39ebb323&format=json&language=en-US&locationType=locale&query='

$('#build').on('click', () => {
  const text = $('#text').val();
  const resultEl = $('#result');
  const uri = `${weatherApi}${encodeURI(text)}`;
  fetch(uri)
    .then(r => r.json())
    .then(r => JSON.stringify(r))
    .then(r => resultEl.html(r))
    .catch(e => alert(e));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <input id='text' type='text'><button id='build'>Search</button>
</div>
<p id='result'></p>

【讨论】:

  • 谢谢! Weather.gov 上的 API(因为 weather.com 没有)是否适用于该项目?
  • 这个特定的站点(weather.com)需要一个特定的API密钥,它可能是由他们生产和提供的。另一家公司提供的密钥应该起作用,但在你尝试之前你永远不会知道 =)
  • 是的,我的意思是用 weather.gov API 重做整个项目
  • 哦,是的,如果他们允许您按邮政编码查询,那肯定会。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 1970-01-01
  • 2017-02-27
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
相关资源
最近更新 更多