1

I have 2 scripts which submits a post request with Ajax parameters.

One script uses the requests libary (script that works). The other one uses urllib & urllib2.

At the moment I have no idea why the urllib script does not work.

Can anybody help?

Script using Request:

import requests
s = requests.Session()
url = "https://www.shirtinator.de/?cT=search/motives&sq=junggesellenabschied"
data1 = {
        'xajax': 'searchBrowse',
        'xajaxr': '1455134430801',
        'xajaxargs[]': ['1', 'true', 'true', 'motives', '100'],
        }
r = s.post(url, data=data1, headers={'X-Requested-With': 'XMLHttpRequest'}, verify=False)
#r = requests.post(url, data=data1, headers={'X-Requested-With': 'XMLHttpRequest'}, verify=False)
#r = requests.post(url, verify=False)
result = r.text
print result
print result.count("motiveImageBox")

Script using urllib:

import urllib2
import urllib
#
url = "https://www.shirtinator.de/?cT=search/motives&sq=junggesellenabschied"
data = ({
        'xajax': 'searchBrowse',
        'xajaxr': '1455134430801',
        'xajaxargs[]': ['1', 'true', 'true', 'motives', '100'],
        })
encode_data = urllib.urlencode(data)
print encode_data
req = urllib2.Request(url,encode_data)
response = urllib2.urlopen(req)
d = response.read()
print d
print d.count("motiveImageBox")
Bob174
  • 33
  • 4
  • Please add a quick description of the error - with stack trace if available. – tdelaney Feb 11 '16 at 15:35
  • the scripts aren't identical - for example script with `requests` has headers `headers={'X-Requested-With': 'XMLHttpRequest'}` which some servers use to recognize `ajax` requests – furas Feb 11 '16 at 15:37
  • 1
    Not sure what you're asking; the `urllib2` version works for me. But you should just stick with `requests` if you can. [This question](http://stackoverflow.com/questions/2018026/should-i-use-urllib-urllib2-or-requests) seems relevant. [The `requests` docs](http://docs.python-requests.org/en/master/) also do a decent job of explaining the virtues of `requests`. – Matt Hall Feb 11 '16 at 15:54

0 Answers0