2

I tried logging into quora using python. But it gives me the following error.

urllib2.HTTPError: HTTP Error 500: Internal Server Error

This is my code till now. I also work behind a proxy.

import urllib2
import urllib
import re
import cookielib

class Quora:
    def __init__(self):
         '''Initialising and authentication'''

         auth = 'http://name:password@proxy:port' 
         cj = cookielib.CookieJar()
         logindata = urllib.urlencode({'email' : 'email' , 'password' : 'password'})
         handler = urllib2.ProxyHandler({'http' : auth})
         opener = urllib2.build_opener(handler , urllib2.HTTPCookieProcessor(cj))
         urllib2.install_opener(opener)
         a = urllib2.urlopen('http://www.quora.com/' , logindata)

def main():
    Quora()

Can someone please point out what is wrong?

if __name__ == '__main__':
    main()
Manoj
  • 961
  • 4
  • 11
  • 37
  • 2
    I wanted to add more code at the later part and this was just the starting. Could you please point out , what is wrong , if you have any idea? @DanielRoseman – Manoj Jan 10 '13 at 14:48

1 Answers1

1

Try something like this:

# Load proxies
proxies = []
proxies_fp = open('proxies.txt', 'r') # A list of proxies
for proxy in proxies_fp:
        proxies.append(proxy)


cookiejar = cookielib.CookieJar()

def perform_request(url, opener, credientials):
        # Instantiate our request object
        request = urllib2.Request(url)

        # Perform the request, returning a pointer to the result set.
        result = opener.urlopen(request, credentials)

        return result

credentials ={
        'username' : 'username',
        'password' : 'password'
        }

encoded_credentials = urllib.urlencode(credentials)

def main():
        # Get random proxy
        proxy = random.choice(proxies)

        # Install our proxy
        opener = urllib2.build_opener(
            urllib2.ProxyHandler({'http': proxy}),
            urllib2.HTTPRedirectHandler(),
            urllib2.HTTPHandler(debuglevel=0),
            urllib2.HTTPSHandler(debuglevel=0),
            urllib2.HTTPCookieProcessor(cookiejar),
            )
        urllib2.install_opener(opener)
        a = perform_request(url, opener, encoded_credentials)

--untested--

I've had to do something similar to this, and it worked for me this way. (Please note, that this is NOT an exact copy of code I used. I had to manipulate it a bit, and did NOT test)

That1Guy
  • 7,075
  • 4
  • 47
  • 59
  • 1
    Thanks for your help . But it still gives me an internal server error . And I couldn't find much difference between your code snippet and mine except for the fact that you have added HTTPHandlers extra .Do you mind trying the code and testing it yourself? – Manoj Jan 11 '13 at 07:37
  • 500 means it is on server side problem, like quora.com, you may put an incorrect parameter, header etc. You need to read their docs carefully. You need to make sure your request call is correct. It is not ur client side code problem – TheOneTeam Jan 13 '13 at 01:49