此代码有什么问题?我尝试使用Python 3.4登录ISP客户端页面并返回相同的html页面。
import urllib.request
import urllib.parse
from http import cookiejar
from bs4 import BeautifulSoup
url = "https://www.exampe.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 = {'unam' : name, 'pass' : passw}
data = urllib.parse.urlencode(values)
binary_data = data.encode('utf-8')
login = urllib.request.Request(url,binary_data,headers)
login_response = opener.open(login)
soup = BeautifulSoup(login_response.read(),'lxml')
print(soup)
Login()
答案 0 :(得分:0)
values = {'unam' : name, 'pass' : passw}
这里假设表单元素名称为unam
和pass
,因站点而异。例如,如果您尝试登录Facebook,则字段名称为email
和pass
。然后必须将该行修改为
values = {'email' : name, 'pass' : passw}
检查您需要登录的网页中的元素,以确定名称并使用它们。
答案 1 :(得分:0)
import urllib.request
import urllib.parse
from http import cookiejar
from bs4 import BeautifulSoup
login = 'user'
password = 'password''
cook = 'mfp.cookies'
url = "https://example/login.php"
cj = cookiejar.MozillaCookieJar(cook)
opener=urllib.request.build_opener(urllib.request.HTTPRedirectHandler(),
urllib.request.HTTPSHandler(),urllib.request.HTTPSHandler(debuglevel=0),
urllib.request.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36'Wink]
values = {'uname' : login,'upass' : password,'login' : 'Login'}
data = urllib.parse.urlencode(values)
binary_data = data.encode('UTF-8')
s = opener.open(url,binary_data)
main = opener.open('http://example.com/page/1/index.php')
soup = BeautifulSoup(main.read(),'lxml')
cj.save()