简体   繁体   中英

Get Headers and post them (requests) PYTHON

im tryng to get response headers and post them to a post.requests to an other url. Sometimes the post requests goes through even without headers, but after trying it for like 2/3 timesi get: 403 Forbidden status, so i'm trying to copy headers from first URL get and post to the second URL post.requests

import requests

url = "https://www.converse.com/it/it/regular/chuck-70-classic-high-top/162050MP.html"
r = requests.get(url)
h = r.headers

cookie = r.cookies
print(r.status_code)
# print(cookie)
print(h)

# ------------------HEADERS

headers2 = {
    'accept': '*/*',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7',
    'cache-control': 'max-age=0',
    'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/89.0.4389.90 Safari/537.36',
    'x-requested-with': 'XMLHttpRequest',
    'authority': 'www.converse.com',
    'referer': 'https://www.converse.com/it/it/regular/patchwork-chuck-70-high-top/170059C_100.html'
}

params = {
    'Quantity': '1',
    'cartAction': 'add',
    'pid': '162050C_095'
}

url2 = 'https://www.converse.com/on/demandware.store/Sites-converse-it-Site/it_IT/Cart-AddProduct?format=ajax'
r2 = requests.post(url2, params=params, headers=h)
print(r2.status_code)

But im getting this:

"C:\desktop\TRU CLI\Scripts\python.exe" C:/Users/matti/PycharmProjects/TroughCLI/getcookie.py
200
{'Server': 'nginx', 'Content-Type': 'text/html;charset=UTF-8', 'Accept-Ranges': 'bytes', 'X-Content-Type-Options': 'nosniff', 'x-dw-request-base-id': 'qJAHbWx_XGABAAB_', 'X-XSS-Protection': '1;mode=block', 'X-FRAME-OPTIONS': 'SAMEORIGIN', 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': 'Thu, 01 Dec 1994 16:00:00 GMT', 'Content-Encoding': 'gzip', 'X-EdgeConnect-MidMile-RTT': '0', 'X-EdgeConnect-Origin-MEX-Latency': '111', 'X-Akamai-Transformed': '9 55032 0 pmb=mTOE,3mRUM,1', 'Date': 'Thu, 25 Mar 2021 13:13:30 GMT', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive, Transfer-Encoding', 'Vary': 'Accept-Encoding', 'Set-Cookie': 'dwac_bct3kiaagPLxsaaadoJd2KwfeA=DjdxDy6ljLQ_EZkAk0kI5JGW93e6s3SsSdA%3D|dw-only|||EUR|false|Etc%2FUTC|true; Path=/, cqcid=bcUh1usyRxPrMFp0IB9awzkFo2; Path=/, cquid=||; Path=/, sid=DjdxDy6ljLQ_EZkAk0kI5JGW93e6s3SsSdA; Path=/, dwanonymous_27bd3e9f26f09719a59d7ae14286db28=bcUh1usyRxPrMFp0IB9awzkFo2; Version=1; Comment="Demandware anonymous cookie for site Sites-converse-it-Site"; Max-Age=15552000; Expires=Tue, 21-Sep-2021 13:13:30 GMT; Path=/, dwsecuretoken_27bd3e9f26f09719a59d7ae14286db28=WcNWWzVMhooYN8kMQZ6tu0PRDU4OdaORSg==; Version=1; Comment="Demandware Secure Token for site Sites-converse-it-Site"; Path=/; Secure; HttpOnly; SameSite=None, __cq_dnt=0; Path=/, dw_dnt=0; Path=/, dwsid=0iLvYSw3EP1G0Z2bi-cG4k2DaRm4IejlMBAG7uDQPinKLIfyEd2L7pTPzMAep00rUCgUAgWL9VSK-VHgLd_lYw==; path=/; HttpOnly, geoloc=cc=IT,rc=,tp=vhigh,tz=GMT+1,la=45.47,lo=9.20; path=/; domain=.www.converse.com, ak_bmsc=624C12013F6830AC885D5AA58C1633AA5C7B651F9F6B00007A8C5C60D65B7D07~plq7bPSVPWBJk2IRTM16allbgUtC0v4hQvJL54zHO74Bj0D0pMZTc4qw2DoqKhtKXbMp9oPpeU1v5ykZZfxkbAdbCd0lRsG2GoTSWmJC7C/tQptOQCJpOhH7XzMSp0pkq7B03y88p5uPxDL9h96FTGNe62LqRS5zV+s2C+3hz+7/HakkT4J51/7prlF3hHQxCT09a8mBZ6cNJ7z6EMc5YgAeI0tY+HCAJDExyJt5ZYMGg=; expires=Thu, 25 Mar 2021 15:13:30 GMT; max-age=7200; path=/; domain=.converse.com; HttpOnly, bm_mi=C495B18776479A3854F3982974B3A080~CpTU3LRpsOcEy73TKNapYJ+M+RBQ8/tT5uP82EWh+6bme/R4dq13737wYoqcVnMaE16qpaN4l31g4xcdEw4U/uTQUQR4c1tjUX2NGgMSBrAIqDSfIMiM/UVrrMMjU1w+DzeHUtxgWB+WVAqz1d35NXnnQQzQCl8vrQdgtNQMXWqXcXVESIbKlV1xOfBf/goaEy7s9142cOlLbbxRGsY41S51FdwoswLt8vbFweK6XL5VE6P4YJwE0SBpZkC5aLbdhsVcx45DXkUGz+6wTEl74nE/6bBue5kuvV0rDK5NzMQ=; Domain=.converse.com; Path=/; Max-Age=0; HttpOnly, bm_sz=40091001B2DEBA22B9752A4692B3D263~YAAQH2V7XHaWfz54AQAAyb2EaQuYE4fKBasvygztJSuulpj+vE7xpTV3pZEh4yKogn7jArFisEQpnpw88UVWm8tMnu75iF2BXejc0fnk0eNhCgcJpwcD9VPlztJc2MpHJkvD0XGtTxJ9celZlzLNP63GJi6ZQsl8M+OukajSRd1Ozia4b3eS1dscK7x6p8CH0LyNUmIK7YplWY6xW/dM5qOYdC4+sWgwlhDqQqsGVtIlDaJeVSoT1d9/aNSFtWZRrXXWRvpH8mzbDY8yx+cR; Domain=.converse.com; Path=/; Expires=Thu, 25 Mar 2021 17:13:30 GMT; Max-Age=14400; HttpOnly, _abck=CF9636A9B1E7DB7898C4613BD1EE636F~-1~YAAQH2V7XHeWfz54AQAAyb2EaQVdoeui6s5GBC3wWZNOtwgdqj5CVkTvpXc3OB9+vTI5h1W8XClJfTbCGbntDQzWAQiKbUjB7LclXM5xZiAfPHVKACZgnfgNPMOM34GPKhq/bHe6Jw5QyDwyhweJG9MRYPuY5CmM96AI/JvyWOvsHCod/Skc2Feq7Izsq7W3q69/dfDFbCfacaheh2FN8WrH0i87A4uYSSKeK/7e1RjhRPeUz9MjBHfAMbormyUyre07vlaFSG4KD8ZJIiIY8tidCcGdw3tNNkg4pwnaqj/XvfRMLWiJrQlUVn0rlM2BNRVZJOPBOqoTZ0ySCsEGlQswB3JTsg==~-1~-1~-1; Domain=.converse.com; Path=/; Expires=Fri, 26 Mar 2021 13:13:30 GMT; Max-Age=86400; Secure', 'Server-Timing': 'cdn-cache; desc=HIT, edge; dur=139'}
Traceback (most recent call last):
  File "C:\desktop\TRU CLI\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
  File "C:\desktop\TRU CLI\lib\site-packages\urllib3\connectionpool.py", line 445, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "C:\desktop\TRU CLI\lib\site-packages\urllib3\connectionpool.py", line 440, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Users\matti\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1347, in getresponse
    response.begin()
  File "C:\Users\matti\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 307, in begin
    version, status, reason = self._read_status()
  File "C:\Users\matti\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 295, in _read_status
    raise BadStatusLine(line)
http.client.BadStatusLine: HTTP/1.1   0 Init


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\desktop\TRU CLI\lib\site-packages\requests\adapters.py", line 439, in send
    resp = conn.urlopen(
  File "C:\desktop\TRU CLI\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen
    retries = retries.increment(
  File "C:\desktop\TRU CLI\lib\site-packages\urllib3\util\retry.py", line 532, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "C:\desktop\TRU CLI\lib\site-packages\urllib3\packages\six.py", line 734, in reraise
    raise value.with_traceback(tb)
  File "C:\desktop\TRU CLI\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
  File "C:\desktop\TRU CLI\lib\site-packages\urllib3\connectionpool.py", line 445, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "C:\desktop\TRU CLI\lib\site-packages\urllib3\connectionpool.py", line 440, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Users\matti\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1347, in getresponse
    response.begin()
  File "C:\Users\matti\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 307, in begin
    version, status, reason = self._read_status()
  File "C:\Users\matti\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 295, in _read_status
    raise BadStatusLine(line)
urllib3.exceptions.ProtocolError: ('Connection aborted.', BadStatusLine('HTTP/1.1   0 Init\r\n'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\matti\PycharmProjects\TroughCLI\getcookie.py", line 34, in <module>
    r2 = requests.post(url2, params=params, headers=h)
  File "C:\desktop\TRU CLI\lib\site-packages\requests\api.py", line 119, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "C:\desktop\TRU CLI\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\desktop\TRU CLI\lib\site-packages\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\desktop\TRU CLI\lib\site-packages\requests\sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "C:\desktop\TRU CLI\lib\site-packages\requests\adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine('HTTP/1.1   0 Init\r\n'))

Process finished with exit code 1
enter code here

How can i fix this?

If your request goes through fine the first 2/3 times, that means that the server is accepting your request fine.

Since Converse is a sneakers site (with demandware) it has antibot to block scripts that try to spam their servers when they drop sneakers (like their OW converse they're about to drop)

My guess is that you're just getting temporarely rate limited/blocked by the site. Try to set a delay ( time.sleep() ) or use proxies and see what happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM