使用python登录到quora [英] Logging into quora using python

查看:125
本文介绍了使用python登录到quora的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用python登录到quora.但这给了我以下错误.

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()

推荐答案

尝试如下操作:

# 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)

-未测试-

我必须做与此类似的事情,并且它以这种方式为我工作. (请注意,这不是我使用的代码的精确副本.我不得不对其进行一点操作,并且没有进行测试)

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)

这篇关于使用python登录到quora的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆