Abul Hasnat
Abul Hasnat

Reputation: 1631

Curl POST in python or similar method

This might be a little too much direct question. New to Python

I am trying to Parse/Scrape video link from a video website(Putlocker).
Ie http://www.putlocker.com/file/A189D40E3E612C50.

The page comes up initially with this code below or similar

<form method="post">
<input type="hidden" value="3d0865fbb040e670" name="hash">
<input name="confirm" type="submit" value="Continue as Free User" 
disabled="disabled"  
 id="submitButton" class="confirm_button" style="width:190px;">
</form>

value="3d0865fbb040e670" Changes everytime...

Import urllib
import urllib2

url = 'http://www.putlocker.com/file/A189D40E3E612C50.'
response = urllib2.urlopen(url)
page = response.read()

from here i find the Value="?" of Hash

then

url = 'http://www.putlocker.com/file/A189D40E3E612C50.'
values = {'hash' : 3d0865fbb040e670}
data = urllib.urlencode(values)
response = urllib2.urlopen(url)
page = response.read()

But I end up on same page again. Do I post value="Continue as Free User" as well? How do I go ahead with posting both data.

A working code would be helpful. I am trying hard but no avail yet.

Ok..after the suggestion made by few programmers

I tried with codes like below

url = 'http://www.putlocker.com/file/A189D40E3E612C50'
response = urllib2.urlopen(url)
html = response.read()
r = re.search('value="([0-9a-f]+?)" name="hash"', html)
session_hash = r.group(1)
print session_hash
form_values = {} 
form_values['hash'] = session_hash
form_values['confirm'] = 'Continue as Free User'
data = urllib.urlencode(form_values)
response = urllib2.urlopen(url, data=data) 
html = response.read()
print html

So I am returned with same page again again..What am I doing wrong here!! I have seen something called pycurl..but I want use something simpler..Any clue??

Upvotes: 1

Views: 545

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1121168

You do need to give your encoded values parameter to the urlopen command:

response = urllib2.urlopen(url, data)

otherwise you will create another GET request instead of POSTing.

Upvotes: 1

Marcus
Marcus

Reputation: 6839

urllib2.urlopen(url,data=data)

Upvotes: 1

Related Questions