-3

Does anyone knows what's wrong in this code ? When I run it I get same HTML page back.

# -*- coding: utf-8 -*-

from http import cookiejar
import urllib.request
import urllib.parse
from bs4 import BeautifulSoup

login = 'username'

password = 'password'
cookiejar = cookiejar.CookieJar()
urlOpener = \
    urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookiejar))
urlOpener.addheaders = [('User-Agent',
                        'Mozilla/5.0 (X11 Linux i686)AppleWebKit/537.36     (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36'
                        )]
values = {'user_name': login, 'user_pass': password, 'login': 'Login'}
data = urllib.parse.urlencode(values)
binary_data = data.encode('utf-8')
request = urllib.request.Request('http://securityoverride.com/login.php'
                                 , binary_data)
url1 = urlOpener.open(request)
url2  = urlOpener.open('http://securityoverride.org/challenges/programmin/1/index.php')
soup = BeautifulSoup(url2.read(), 'lxml')
print soup.find_all('center')
blue112
  • 52,634
  • 3
  • 45
  • 54
Poke
  • 11
  • 1
  • 2
  • 6
  • Indention and quotations are your problems here. Fix those and we may be able to help a little more. – Andy Oct 08 '14 at 13:25
  • Use requests instead of urllib. Requests is like a wrapper for urllib. It makes it a lot easier. http://docs.python-requests.org/en/latest/ I'm totally adicted to it :P – Vincent Beltman Oct 08 '14 at 13:28
  • it's not possible using urllib – Poke Oct 08 '14 at 13:31
  • There’s also a CSRF token inserted at run-time. It has a quite useless/broken value but maybe the server checks for its presence. – poke Oct 08 '14 at 14:13

2 Answers2

0

Login into a web applicaction by script may be trivial or awfully complex, depending on how the login page is organized. In my experience, the only foolproof way is :

  • use a network spy like wireshark
  • spy a full successful login sequence from a true browser
  • until successful loop :
    • try to reproduce it with the script
    • spy the login sequence from the script and analyze differences

IMHO using urllib2 or requests make little differences. The only tool that is much better at that would be mechanize, but unfortunately it seems not to be ported to Python 3

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0
       urllib.request
       import urllib.parse
       from http import cookiejar
       from bs4 import BeautifulSoup

      url = "http://securityoverride.com/login.php"
      name = "username"
      passw = "password"

      def Login():
       cj = cookiejar.CookieJar()
       user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
       headers = { 'User-Agent' : user_agent }
       redirect =urllib.request.HTTPRedirectHandler()
       opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
       values = {'user_name': login, 'user_pass': password, 'login': 'Login'}
       data = urllib.parse.urlencode(values)
       binary_data = data.encode('utf-8')
       login = urllib.request.Request(url,binary_data,headers)
       login_response = opener.open(login)
       url2  = urlOpener.open('http://securityoverride.org/challenges/programmin/1/index.php')
       soup = BeautifulSoup(url2.read(), 'lxml')
       print soup.find_all('center')

Login()
P_O_I_S_O_N
  • 357
  • 5
  • 14