以下是代码:
quote_page = "https://www.timeanddate.com/holidays/fun/" + months [date.month].lower() + "/" + str(date.day)
page = urllib2.urlopen(quote_page)
soup = BeautifulSoup(page, 'html.parser')
event_box = soup.find('article', attrs={"class" : "fixed"})
event_box = event_box.find('h3')
event = event_box.text.strip()
print event
当我检查我的变量时:
quote_page = https://www.timeanddate.com/holidays/fun/june/8
我已尝试打印quote_page,链接在我的浏览器中正常工作, 但是当我运行代码并打印“事件”时,我得到了这个:
Traceback (most recent call last):
File "main.py", line 252, in <module>
page = urllib2.urlopen(req)
File "/usr/local/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/lib/python2.7/urllib2.py", line 435, in open
response = meth(req, response)
File "/usr/local/lib/python2.7/urllib2.py", line 548, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/local/lib/python2.7/urllib2.py", line 473, in error
return self._call_chain(*args)
File "/usr/local/lib/python2.7/urllib2.py", line 407, in _call_chain
result = func(*args)
File "/usr/local/lib/python2.7/urllib2.py", line 556, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 403: ok
exit status 1
我想我记得它今天早些时候工作但是后来我再次尝试时停止了。
有人可以帮忙吗? (我对编码很新)