【问题标题】:Remove 'urllib.error.HTTPError: HTTP Error 302:' from urlReq(url)从 urlReq(url) 中删除 'urllib.error.HTTPError: HTTP Error 302:'
【发布时间】:2019-08-20 05:28:39
【问题描述】:

大家好,怎么了? :)
我正在尝试使用一些 url 参数来抓取网站。 如果我正确使用 url1, url2, url3WORKS 并打印出我想要的常规输出 (html) ->

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

# create urls
url1 = 'https://en.titolo.ch/sale'
url2 = 'https://en.titolo.ch/sale?limit=108'
url3 = 'https://en.titolo.ch/sale?category_styles=29838_21212'
url4 = 'https://en.titolo.ch/sale?category_styles=31066&limit=108'

# opening up connection on each url, grabbing the page
uClient = urlReq(url4)
page_html = uClient.read()
uClient.close()

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

# print the html
print(page_soup.body.prettify())

-> 但是当我尝试 "url4" url4 = 'https://en.titolo.ch/sale?category_styles=31066&limit=108' 它给了我下面的错误。我究竟做错了什么?
- 也许它与饼干有关? -> 但为什么它在其他网址上有效...
- 也许他们只是在阻止抓取尝试?
- 如何通过在 URL 中使用多个参数来避免此错误?

urllib.error.HTTPError: HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
Moved Temporarily

提前感谢您的帮助! 干杯 艾伦

我已经尝试过的: 我尝试了请求库

import requests

url = 'https://en.titolo.ch/sale?category_styles=31066&limit=108'
r = requests.get(url)
html = r.text
print(html)

<!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 /sale
on this server.</p>
</body></html>

[Finished in 0.375s]

来自 urllib 请求的完整错误消息:

Traceback (most recent call last):
  File "C:\Users\jedi\Documents\non\of\your\business\smile\stackoverflow_question", line 12, in <module>
    uClient = urlReq(url4)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 531, in open
    response = meth(req, response)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 563, in error
    result = self._call_chain(*args)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 755, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 531, in open
    response = meth(req, response)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 563, in error
    result = self._call_chain(*args)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 755, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 531, in open
    response = meth(req, response)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 563, in error
    result = self._call_chain(*args)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 755, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 531, in open
    response = meth(req, response)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 563, in error
    result = self._call_chain(*args)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 755, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 531, in open
    response = meth(req, response)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 563, in error
    result = self._call_chain(*args)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "C:\Users\jedi\AppData\Local\Programs\Python\Python37\lib\urllib\request.py", line 745, in http_error_302
    self.inf_msg + msg, headers, fp)
urllib.error.HTTPError: HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
Moved Temporarily
[Finished in 2.82s]

【问题讨论】:

    标签: python url cookies beautifulsoup urllib


    【解决方案1】:

    如果使用requests 包并在标头中添加用户代理,看起来它会为所有 4 个链接获得200 响应。所以尝试添加用户代理标头:

    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'}

    import requests
    from bs4 import BeautifulSoup as soup
    
    # create urls
    url1 = 'https://en.titolo.ch/sale'
    url2 = 'https://en.titolo.ch/sale?limit=108'
    url3 = 'https://en.titolo.ch/sale?category_styles=29838_21212'
    url4 = 'https://en.titolo.ch/sale?category_styles=31066&limit=108'
    
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'}
    
    url_list = [url1, url2, url3, url4]
    
    for url in url_list:
    # opening up connection on each url, grabbing the page
        response = requests.get(url, headers=headers)
        print (response.status_code)
    

    输出:

    200
    200
    200
    200
    

    所以:

    import requests
    
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'}
    
    url = 'https://en.titolo.ch/sale?category_styles=31066&limit=108'
    
    r = requests.get(url, headers=headers)
    html = r.text
    print(html)
    

    【讨论】:

    • 非常感谢!!它工作正常。我对http不熟悉,所以这对我很有帮助!周末愉快。 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 2012-10-07
    • 2021-08-01
    • 1970-01-01
    • 2022-11-04
    • 2016-10-01
    • 2021-04-13
    相关资源
    最近更新 更多