1

I am making a program in Python 2.7 which I want to POST a URL to the browser. Here is my code which should explain it much better than I can:

import requests, json, webbrowser

pid = "AQ6723"
size = "660"
recaptcha = ""

baseURL = 'http://www.adidas.co.uk/on/demandware.store/Sites-adidas-GB-Site/en_GB/Cart-MiniAddProduct'

payload = {
            'dwfrm_cart_continueShopping': 'Continue+Shopping',
            'layer': 'Add+To+Bag+overlay',
            'pid': '%20' + pid + '_' + size,
            'pid': '%20' + pid + '_' + size,
            'g-recaptcha-response': recaptcha,
            'Quantity': "1",
            'masterPid': pid,
            'ajax': "true"
        }

headers = {
            'Host': 'www.adidas.co.uk',
            'Connection': 'keep-alive',
            'Content-Length': '85',
            'Accept': '*/*',
            'Origin': 'http://www.adidas.co.uk',
            'X-Requested-With': 'XMLHttpRequest',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'en-US,en;q=0.8,de;q=0.6',
        }

print(pid)

finishedProduct = requests.post(baseURL, data = json.dumps(payload), headers = headers)

webbrowser.open(finishedProduct)

This obviously isn't correct but how could I "achieve" this? I just want to be able to see the result of the POST request in browser which ultimately would be a product in-cart.

Daniel Iverson
  • 119
  • 1
  • 10
  • `requests.post` returns a response object. `webbrowser.open` of course cannot understand it. It expects a url – Anthony Kong Apr 15 '16 at 22:49
  • Yes.. I know that that obviously isn't correct it was more of a display of what I'm trying to achieve. Is there a way I could do something like that requests.post to the webbrowser? – Daniel Iverson Apr 15 '16 at 23:10
  • Then you need to understand what these APIs work. Maybe there is a separate API that can report the cart content. – Anthony Kong Apr 16 '16 at 00:20

1 Answers1

1

There is no way of doing this in python natively. If you are looking to automate your browser, look in to Selenium. Selenium has a python binding.

ToddJCrane
  • 1,587
  • 1
  • 14
  • 22