2014-02-07 2 views
0

그래서 나는 주위를 찾고있다 함께 나 웹 사이트에 로그인 할 수 있습니다 몇 가지 코드, http://forums.somethingawful.com 그것은 작동urllib2가, 파이썬, 특정 사이트를 열고 쓰레기 응답

, 내가 응답이에서 볼 수 자갈 관리해야 그것은 작동합니다.

내가 위의 로그인을 위해 만든 동일한 urllib2 따개를 사용하려고하면이 페이지를 열려면이 http://forums.somethingawful.com/attachment.php?attachmentid=300 (이 웹 사이트를 방문하려면 로그인해야합니다)의이 부분을 방문하려면 " ÿØÿà "

편집 :이 더 이상

왜 어떤 아이디어가 도움이되는 경우 http://i.imgur.com/PmWl1s4.png

내가 대상 페이지에서 로그인 할 때 어떻게 생겼는지의 스크린 샷을 포함했다?

""" 
# Script to log in to website and store cookies. 
# run as: python web_login.py USERNAME PASSWORD 
# 
# sources of code include: 
# 
# http://stackoverflow.com/questions/2954381/python-form-post-using-urllib2-also-question-on-saving-using-cookies 
# http://stackoverflow.com/questions/301924/python-urllib-urllib2-httplib-confusion 
# http://www.voidspace.org.uk/python/articles/cookielib.shtml 
# 
# mashed together by Martin Chorley 
# 
# Licensed under a Creative Commons Attribution ShareAlike 3.0 Unported License. 
# http://creativecommons.org/licenses/by-sa/3.0/ 
""" 

import urllib, urllib2 
import cookielib 
import sys 

import urlparse 
from BeautifulSoup import BeautifulSoup as bs 

class WebLogin(object): 

    def __init__(self, username, password): 

     # url for website we want to log in to 
     self.base_url = 'http://forums.somethingawful.com/' 
     # login action we want to post data to 
     # could be /login or /account/login or something similar 
     self.login_action = '/account.php?' 
     # file for storing cookies 
     self.cookie_file = 'login.cookies' 

     # user provided username and password 
     self.username = username 
     self.password = password 

     # set up a cookie jar to store cookies 
     self.cj = cookielib.MozillaCookieJar(self.cookie_file) 

     # set up opener to handle cookies, redirects etc 
     self.opener = urllib2.build_opener(
      urllib2.HTTPRedirectHandler(), 
      urllib2.HTTPHandler(debuglevel=0), 
      urllib2.HTTPSHandler(debuglevel=0), 
      urllib2.HTTPCookieProcessor(self.cj) 
     ) 

     # pretend we're a web browser and not a python script 
     self.opener.addheaders = [('User-agent', 
      ('Chrome/16.0.912.77')) 
     ] 

     # open the front page of the website to set and save initial cookies 
     response = self.opener.open(self.base_url) 
     self.cj.save() 

     # try and log in to the site 
     response = self.login() 

     response2 = self.opener.open("http://forums.somethingawful.com/attachment.php?attachmentid=300") 

     print response2.read() + "LLLLLL" 

    # method to do login 
    def login(self): 

     # parameters for login action 
     # may be different for different websites 
     # check html source of website for specifics 
     login_data = urllib.urlencode({ 
       'action': 'login', 
       'username': 'username', 
       'password': 'password' 
     }) 

     # construct the url 
     login_url = self.base_url + self.login_action 
     # then open it 
     response = self.opener.open(login_url, login_data) 
     # save the cookies and return the response 
     self.cj.save() 
     return response 

if __name__ == "__main__": 

    username = "username" 
    password = "password" 

    # initialise and login to the website 
    test = WebLogin(username, password) 
+0

실행 가능한 예제가 없으면 무엇이 잘못되었는지 설명하기가 어렵습니다. – Blender

+0

@Blender 죄송합니다. 소스와 함께 로그인 할 때 대상 페이지가 어떻게 보이는지에 대한 스크린 샷으로 질문을 업데이트했습니다. 나는 이것이 아직도 도움이되지 않는다는 것을 안다. 그러나 나의 로그인 자격 증명을 게시하는 것의 부족 나는 내가 지금 할 수있는 그 밖의 많은 것들이 없다고 생각한다. – user1079404

답변

1

대신을 시도해보십시오

import urllib2,cookielib 

def login(username,password): 
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar())) 
    url1 = "http://forums.somethingawful.com/attachment.php?attachmentid=300" 
    url2 = "http://forums.somethingawful.com/account.php?action=loginform" 
    data = "&username="+username+"&password="+password 
    socket = opener.open(url1) 
    socket = opener.open(url2,data) 
    return socket.read() 

P.S을 : 나는 독립 함수로 쓴; 그것은 당신을 위해 작동한다면 당신은 당신의 수업에 그것을 통합 할 수 있습니다. 또한 opener.open(url1)에 대한 호출이 중복 될 수 있습니다. 그걸 확인하기 위해 유효한 사용자 이름/암호 쌍이 필요합니다.

+0

안녕하세요. 이 코드를 통해 얻을 수있는 응답은 로그인 페이지입니다. 따라서 어떤 이유로이 방법으로 로그인하지 못합니다. 로그인 자격 증명없이 할 수있는 일이 많지 않으므로 고맙습니다. 감사합니다. – user1079404

+0

안녕하세요. 게시물이 도움이된다고 생각하면 게시물을 올리는 것을 잊지 마시고, 질문에 답하면 게시물을 수락하십시오. –

관련 문제