【问题标题】:Connection aborted.', RemoteDisconnected('Remote end closed connection without response') while using python连接中止。',RemoteDisconnected('远程结束关闭连接没有响应'),而使用python
【发布时间】:2021-03-24 20:41:21
【问题描述】:

您好,我正在尝试在 python 中使用请求时通过会话访问https://api.louisvuitton.com/api/eng-us/catalog/availability/M80016 。目前我无法访问它并收到 Remote end closed connection without response 的错误。 我一直在尝试调试,但没有成功。 Bellow 是我的代码和输出。

代码:

import requests
from requests.auth import HTTPBasicAuth
import json


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

s = requests.Session()
r = s.get("https://us.louisvuitton.com/eng-us/products/pocket-organizer-damier-graphite-nvprod2630093v#N60432",headers=headers)

if r:
    print("Requested Successfully")
else:
    print("Request Failed ==> " + str(r))
    exit()

url2 = "https://api.qubit.com/graphql"

payload = json.dumps({
  "query": "query ($trackingId: String!, $contextId: String!) {\n property(trackingId: $trackingId) {\n   visitor(contextId: $contextId) {\n     ipAddress\n     ipLocation: location {\n       city\n       cityCode\n       country\n       countryCode\n       latitude\n       longitude\n       area\n       areaCode\n       region\n       regionCode\n     }\n     segment: segments {\n       state\n     }\n     history {\n       conversionCycleNumber: conversionCycle\n       conversionNumber: conversions\n       entranceNumber: entrances\n       firstConversionTs: firstConversion\n       firstViewTs: firstView\n       lastConversionTs: lastConversion\n       lastViewTs: lastView\n       lifetimeValue\n       sessionNumber: sessions\n       viewNumber: views\n     }\n   }\n }\n}",
  "variables": {
   "trackingId": "louisvuitton_prod",
   "contextId": "o6vfrf9jm4g-0k999shdp-fiadwa4"
  }})

headers2 = {
  'Content-Type': 'application/json'
 }

x = s.post(url2,headers=headers2, data=payload)

if x:
    print("Post Successfully")
else:
    print("Post Failed ==> " + str(x))
    exit()

 
headers3 = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)',
    'Accept': "*/*",
    'Cache-Control': "no-cache",
    'Host': "api.louisvuitton.com",
    'Accept-Encoding': "gzip, deflate",
    'Connection': "keep-alive",
    'cache-control': "no-cache",
    'Content-Type': 'application/json'
    }

cookies = s.cookies

t = s.get("https://api.louisvuitton.com/api/eng-us/catalog/availability/M80016",headers=headers3,cookies=cookies)

if t:
    print("Get Successfully")
else:
    print("Get Failed ==> " + str(t))
    exit()

输出

Requested Successfully
Post Successfully
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/urllib3-1.25.10-py3.8.egg/urllib3/connectionpool.py", line 670, in urlopen
    httplib_response = self._make_request(
  File "/usr/local/lib/python3.8/site-packages/urllib3-1.25.10-py3.8.egg/urllib3/connectionpool.py", line 426, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/usr/local/lib/python3.8/site-packages/urllib3-1.25.10-py3.8.egg/urllib3/connectionpool.py", line 421, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1347, in getresponse
    response.begin()
  File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 307, in begin
    version, status, reason = self._read_status()
  File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 276, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

有人知道如何解决这个问题吗?将不胜感激任何帮助。

【问题讨论】:

    标签: python-3.x http python-requests


    【解决方案1】:

    如果您在 Chrome 中使用 Inspect Element -> application -> storage -> cookies -> https://us.louisvuitton.com/ 检查网页上的 cookie,您会看到大约 40 个 cookie。但是,如果您在代码中添加 import pprint 并且在第 50 行 pprint.pprint(s.cookies.get_dict()) 您只会看到 4 个 cookie。所以你错过了很多 cookie。

    如果您在https://api.louisvuitton.com/api/eng-us/catalog/availability/nvprod.. 上使用Inspect Element -> Network 复制为cURL,您得到的响应实际上是拒绝访问消息。 URL 并删除除您的 4 之外的 cookie 并运行它,如果您运行它,所有 cookie 都可以正常工作。

    由于有许多 XHR 请求无法设置 cookie,我建议您要么检查所有请求,如果需要,解码它们并阅读所有 JavaScript 文件以查看它们是否设置了 cookie,或者使用 Selenium 更简单的解决方案,requests-html @ 987654323@ 或 PyQT

    【讨论】:

      猜你喜欢
      • 2020-12-30
      • 1970-01-01
      • 2021-04-09
      • 2018-12-27
      • 2021-10-17
      • 2017-03-12
      • 2021-08-20
      • 1970-01-01
      • 2018-06-14
      相关资源
      最近更新 更多