爬取网页过程报错:RemoteDisconnected: Remote end closed connection without response

有问题的代码:

import urllib.request
# 代码002:(未声明headers,会报错)
response = urllib.request.urlopen("http://www.baidu.com")
print(response)
print(response.read())

运行结果:

Traceback (most recent call last):
  File "D:/PycharmProjects/pythonProject/douban/test1/temp.py", line 11, in <module>
    response = urllib.request.urlopen("http://www.baidu.com")
  File "C:\Users\lord\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\lord\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 525, in open
    response = self._open(req, data)
  File "C:\Users\lord\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 542, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "C:\Users\lord\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 502, in _call_chain
    result = func(*args)
  File "C:\Users\lord\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 1379, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Users\lord\AppData\Local\Programs\Python\Python38-32\lib\urllib\request.py", line 1354, in do_open
    r = h.getresponse()
  File "C:\Users\lord\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 1332, in getresponse
    response.begin()
  File "C:\Users\lord\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 303, in begin
    version, status, reason = self._read_status()
  File "C:\Users\lord\AppData\Local\Programs\Python\Python38-32\lib\http\client.py", line 272, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

中文翻译:

raise RemoteDisconnected("Remote end closed connection without http.client.RemoteDisconnected: Remote end closed connection without response
远程端未连接(没有http服务器,远程端关闭连接),远程端关闭连接,无响应。

解决方法:

网络参考资源:

网址:《爬虫过程报错:http.client.RemoteDisconnected: Remote end closed connection without response》

利用 urllib 发起的请求,UA 默认是 Python-urllib/3.5 而在 chrome 中访问 UA 则是 User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36,因为服务器根据 UA 来判断拒绝了 python 爬虫。
把 python 伪装成 chrome 去获取糗百的网页,可以顺利的得到数据。

修改后的代码:

import urllib.request

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'}
# 代码001:
data1 = urllib.request.Request("https://www.bilibili.com", headers=headers)
data2 = urllib.request.urlopen(data1).read()
data3 = urllib.request.urlopen(data1)
data4 = urllib.request.urlopen(data1).read().decode('utf-8') #转码
print(data2)
# print(data3)
# print(data4)

# 代码002:(未声明headers,会报错)
# response = urllib.request.urlopen("http://www.baidu.com")
# print(response)
# print(response.read())

headers 获取方法(google):

打开google浏览器,查看网页源代码
在这里插入图片描述
网络参考资源:
《python爬虫-通过chrome浏览器查看当前网页的http头》

这个错误通常表示客户端(即您的程序)与服务器之间的连接意外断开,而服务器没有返回任何内容或响应。这可能是由于以下一些原因导致的: 1. 服务器端发生了错误或崩溃,导致无法正常处理请求并返回响应。 2. 客户端与服务器之间的网络连接中断或出现故障,导致连接意外关闭。 3. 客户端发送的请求不符合服务器的要求或协议,导致服务器无法正常处理请求并关闭连接。 如果您遇到了这个错误,可以尝试以下几个步骤: 1. 检查服务器是否正常工作,并且能够正常处理其他请求。 2. 检查客户端与服务器之间的网络连接是否正常,并且没有出现故障。 3. 检查您的程序是否按照服务器的要求或协议发送请求,并且没有出现错误或不规范的情况。 4. 如果以上步骤都没有解决问题,您可以尝试使用其他方式(例如浏览器或其他客户端程序)发送相同的请求,以便确定问题是否出在您的程序中。 例如,以下代码演示了如何使用 `requests` 库向服务器发送 GET 请求,并处理可能出现的 `RemoteDisconnected` 错误: ```python import requests url = 'https://example.com' try: response = requests.get(url) response.raise_for_status() print(response.content) except requests.exceptions.RequestException as e: print(e) ``` 在这个示例中,我们使用 `requests` 库向 `https://example.com` 发送 GET 请求,并使用 `raise_for_status()` 方法检查是否出现了 HTTP 错误。如果出现了 `RemoteDisconnected` 错误,将会捕获该异常并输出异常信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值