Python:URL库错误,HTTP错误:HTTP错误404:未找到

tvz2xvvm  于 2023-01-03  发布在  Python
关注(0)|答案(5)|浏览(150)

我写了一个脚本来查找SO问题标题中的拼写错误。我用了大约一个月。这个工作很好。
但现在,当我试着运行它时,我得到了这个。

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://stackoverflow.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://stackoverflow.com" + qn['link']
for qn in result:
    print(qn['link'], " Error Words:", qn['words'])
    url = qn['link']

更新

这是导致问题的url。即使此url存在。
https://stackoverflow.com/questions?page=298314&sort=active
我试着把范围改成一些较低的值,现在效果很好。
为什么上面的网址会出现这种情况?

pjngdqdw

pjngdqdw1#

显然,每页默认显示的问题数是50,因此您在循环中定义的范围超出了每页50个问题的可用页数,该范围应调整为每页50个问题的总页数。
这段代码将捕获404错误,这是您得到错误的原因,并忽略它,以防您超出范围。

from urllib.request import urlopen

def find_bad_qn(a):
    url = "https://stackoverflow.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)
nzk0hqpo

nzk0hqpo2#

我有完全相同的问题。我想用urllib获取的url存在,可以用普通浏览器访问,但urllib告诉我404。
我的解决方案是不使用urllib:

import requests
requests.get(url)

这对我有用。

w6lpcovy

w6lpcovy3#

默认的"用户代理"似乎没有Mozilla那么多的访问权限。
尝试导入请求并将, headers={'User-Agent': 'Mozilla/5.0'}附加到URL的末尾。
即:

from urllib.request import Request, urlopen    
url = f"https://stackoverflow.com/questions?page={str(a)}&sort=active"    
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})    
html = urlopen(req)
h7wcgrx3

h7wcgrx34#

这是因为网址不存在,请重新检查您的网址。我也有同样的问题,在重新检查,我发现我的网址是不正确的,然后我改变了它

gcxthw6b

gcxthw6b5#

通过点击链接检查。也许它存在于代码中,这意味着你的代码没有问题,但实际上链接或网站不存在,没有找到。

相关问题