Python: urllib.error.HTTPError: HTTP Error 404: Not Found
Python: urllib.error.HTTPError: HTTP Error 404: Not Found
我写了一个脚本来查找 SO 问题标题中的拼写错误。
我用它大约 month.This 工作正常。
但是现在,当我尝试 运行 它时,我明白了。
Traceback (most recent call last):
File "copyeditor.py", line 32, in <module>
find_bad_qn(i)
File "copyeditor.py", line 15, in find_bad_qn
html = urlopen(url)
File "/usr/lib/python3.4/urllib/request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.4/urllib/request.py", line 469, in open
response = meth(req, response)
File "/usr/lib/python3.4/urllib/request.py", line 579, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.4/urllib/request.py", line 507, in error
return self._call_chain(*args)
File "/usr/lib/python3.4/urllib/request.py", line 441, in _call_chain
result = func(*args)
File "/usr/lib/python3.4/urllib/request.py", line 587, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
这是我的代码
import json
from urllib.request import urlopen
from bs4 import BeautifulSoup
from enchant import DictWithPWL
from enchant.checker import SpellChecker
my_dict = DictWithPWL("en_US", pwl="terms.dict")
chkr = SpellChecker(lang=my_dict)
result = []
def find_bad_qn(a):
url = "https://whosebug.com/questions?page=" + str(a) + "&sort=active"
html = urlopen(url)
bsObj = BeautifulSoup(html, "html5lib")
que = bsObj.find_all("div", class_="question-summary")
for div in que:
link = div.a.get('href')
name = div.a.text
chkr.set_text(name.lower())
list1 = []
for err in chkr:
list1.append(chkr.word)
if (len(list1) > 1):
str1 = ' '.join(list1)
result.append({'link': link, 'name': name, 'words': str1})
print("Please Wait.. it will take some time")
for i in range(298314,298346):
find_bad_qn(i)
for qn in result:
qn['link'] = "https://whosebug.com" + qn['link']
for qn in result:
print(qn['link'], " Error Words:", qn['words'])
url = qn['link']
更新
虽然 url 存在,但这是导致 problem.Even 的 url。
https://whosebug.com/questions?page=298314&sort=active
我尝试将范围更改为一些较低的值。现在可以正常使用了。
为什么上面 url 会发生这种情况?
显然,每页默认显示的问题数是 50,因此您在循环中定义的范围超出了每页 50 个问题的可用页数。范围应调整为每页 50 个问题的总页数。
此代码将捕获 404 错误,这是您遇到错误的原因并忽略它,以防您超出范围。
from urllib.request import urlopen
def find_bad_qn(a):
url = "https://whosebug.com/questions?page=" + str(a) + "&sort=active"
try:
urlopen(url)
except:
pass
print("Please Wait.. it will take some time")
for i in range(298314,298346):
find_bad_qn(i)
我也有同样的问题。我想使用 urllib 获得的 url 存在并且可以使用普通浏览器访问,但是 urllib 告诉我 404。
我的解决方案是不使用 urllib:
import requests
requests.get(url)
这对我有用。
默认的 'User-Agent' 似乎没有 Mozilla 那样多的访问权限。
尝试导入请求并将 , headers={'User-Agent': 'Mozilla/5.0'}
附加到 url 的末尾。
即:
from urllib.request import Request, urlopen
url = f"https://whosebug.com/questions?page={str(a)}&sort=active"
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
html = urlopen(req)
这是因为 URL 不存在,请重新检查您的 URL。我在重新检查时也遇到了同样的问题,我发现我的 URL 不正确,然后我更改了它
我写了一个脚本来查找 SO 问题标题中的拼写错误。 我用它大约 month.This 工作正常。
但是现在,当我尝试 运行 它时,我明白了。
Traceback (most recent call last):
File "copyeditor.py", line 32, in <module>
find_bad_qn(i)
File "copyeditor.py", line 15, in find_bad_qn
html = urlopen(url)
File "/usr/lib/python3.4/urllib/request.py", line 161, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.4/urllib/request.py", line 469, in open
response = meth(req, response)
File "/usr/lib/python3.4/urllib/request.py", line 579, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.4/urllib/request.py", line 507, in error
return self._call_chain(*args)
File "/usr/lib/python3.4/urllib/request.py", line 441, in _call_chain
result = func(*args)
File "/usr/lib/python3.4/urllib/request.py", line 587, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
这是我的代码
import json
from urllib.request import urlopen
from bs4 import BeautifulSoup
from enchant import DictWithPWL
from enchant.checker import SpellChecker
my_dict = DictWithPWL("en_US", pwl="terms.dict")
chkr = SpellChecker(lang=my_dict)
result = []
def find_bad_qn(a):
url = "https://whosebug.com/questions?page=" + str(a) + "&sort=active"
html = urlopen(url)
bsObj = BeautifulSoup(html, "html5lib")
que = bsObj.find_all("div", class_="question-summary")
for div in que:
link = div.a.get('href')
name = div.a.text
chkr.set_text(name.lower())
list1 = []
for err in chkr:
list1.append(chkr.word)
if (len(list1) > 1):
str1 = ' '.join(list1)
result.append({'link': link, 'name': name, 'words': str1})
print("Please Wait.. it will take some time")
for i in range(298314,298346):
find_bad_qn(i)
for qn in result:
qn['link'] = "https://whosebug.com" + qn['link']
for qn in result:
print(qn['link'], " Error Words:", qn['words'])
url = qn['link']
更新
虽然 url 存在,但这是导致 problem.Even 的 url。
https://whosebug.com/questions?page=298314&sort=active
我尝试将范围更改为一些较低的值。现在可以正常使用了。
为什么上面 url 会发生这种情况?
显然,每页默认显示的问题数是 50,因此您在循环中定义的范围超出了每页 50 个问题的可用页数。范围应调整为每页 50 个问题的总页数。
此代码将捕获 404 错误,这是您遇到错误的原因并忽略它,以防您超出范围。
from urllib.request import urlopen
def find_bad_qn(a):
url = "https://whosebug.com/questions?page=" + str(a) + "&sort=active"
try:
urlopen(url)
except:
pass
print("Please Wait.. it will take some time")
for i in range(298314,298346):
find_bad_qn(i)
我也有同样的问题。我想使用 urllib 获得的 url 存在并且可以使用普通浏览器访问,但是 urllib 告诉我 404。
我的解决方案是不使用 urllib:
import requests
requests.get(url)
这对我有用。
默认的 'User-Agent' 似乎没有 Mozilla 那样多的访问权限。
尝试导入请求并将 , headers={'User-Agent': 'Mozilla/5.0'}
附加到 url 的末尾。
即:
from urllib.request import Request, urlopen
url = f"https://whosebug.com/questions?page={str(a)}&sort=active"
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
html = urlopen(req)
这是因为 URL 不存在,请重新检查您的 URL。我在重新检查时也遇到了同样的问题,我发现我的 URL 不正确,然后我更改了它