python programm to log into the web page

后端 未结 2 2086
梦如初夏
梦如初夏 2021-01-29 14:45

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.reques         


        
2条回答
  •  日久生厌
    2021-01-29 15:33

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

提交回复
热议问题