帮助Python:登录网站

时间:2021-09-24 21:49:59

I'm trying to connect to a website with Python, but I don't know how to do that.

我正在尝试使用Python连接到一个网站,但我不知道该怎么做。

The HTML of the website looks like that :

该网站的HTML看起来像这样:

<form action="connexion.php" method="post">
  User<br />
  <input name="login_user" type="text" />
  <br />
  Password<br />
  <input name="pass_word" type="password" />
  <br />
  <input value="Connexion" type="submit" />
</form>

Ok, I will try to explain better ...

好的,我会尝试更好地解释......

Here is the script to connect to a pun_bb forum :

这是连接到pun_bb论坛的脚本:

import urllib
import string

host = "http://punbbforum.example.com"
username = "userxxx"
password = "passwww"
params = {
    'req_username' : username,
    'req_password' : password,
    'form_sent' : 1
    }

wclient = urllib.URLopener()

req = wclient.open(host + "/login.php?action=in", urllib.urlencode(params))
info = req.info()

if 'set-cookie' not in info:
    sys.exit(-3)

cookie = info['set-cookie']

cookie = cookie[:string.find(cookie, ';')]
wclient.addheader('Cookie', cookie);

req = wclient.open("http://punbbforum.example.com/post.php?fid=15")
print req.read()

But it works only with punbb websites ... I would like to create a script who it will work with this example ...

但它只适用于punbb网站...我想创建一个脚本,它将使用此示例...

Thanks

谢谢

3 个解决方案

#1


1  

I think the mechanize module is what you need. It was designed to act as a human with a browser would - allowing you to "click" links, submit forms, login into websites, etc.

我认为机械化模块就是您所需要的。它被设计为充当浏览器的人 - 允许您“点击”链接,提交表单,登录网站等。

#2


1  

You can use urllib Module to open any website and get its HTML code. but if you want to use python to receive data from your HTML Form, you should learn one of web frameworks like: Django, webpy or Flask.

您可以使用urllib模块打开任何网站并获取其HTML代码。但是如果你想使用python从你的HTML表单接收数据,你应该学习一个Web框架,如:Django,webpy或Flask。

Edit: You should replace "req_username" with "login_user", and "req_password" with "pass_word".

编辑:您应该将“req_username”替换为“login_user”,将“req_password”替换为“pass_word”。

#3


1  

I've used for this PyQt. The window has a browser widget in which it loads the web page. After that if fills needed fields (user name and password) and submits the form. I think this solution has advantage over simple form submit that it works with login pages with Javascript making changes to the page after it has loaded.

我用过这个PyQt。该窗口有一个浏览器小部件,用于加载网页。之后,如果填写所需的字段(用户名和密码)并提交表单。我认为这个解决方案优于简单的表单提交,它适用于登录页面,Javascript在加载后对页面进行了更改。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
url = "http://192.168.173.66:8080"
username = "c58941376"
password = "7758"

import sys, signal

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

def JSEval(code):
    return webpage.mainFrame().evaluateJavaScript(code)

def onLoadStarted():
    print "Loading started: %s" % webpage.mainFrame().url().toString()

def onLoadFinished(result):
    print "Loading finished: %s" % webpage.mainFrame().url().toString()
    if not result:
        #print "Request failed"
        return

    JSEval("_form = document.getElementsByName('authenticateForm')[0];")
    if JSEval("_form.tagName").toString() != "FORM" :
        print "Couldn't find needed form. Not a login page (wrong address or already logged in)."
        return
    JSEval("_form.username.value='%s';" % username \
        + "_form.password.value='%s';" % password \
        + "_form.submit();")
    print "Login data sent"


app = QApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)

webpage = QWebPage()
webpage.connect(webpage, SIGNAL("loadFinished(bool)"), onLoadFinished)
webpage.connect(webpage, SIGNAL("loadStarted()"), onLoadStarted)
webpage.mainFrame().load(QUrl(url))

web = QWebView()
web.setPage(webpage)
web.show()

sys.exit(app.exec_())

#1


1  

I think the mechanize module is what you need. It was designed to act as a human with a browser would - allowing you to "click" links, submit forms, login into websites, etc.

我认为机械化模块就是您所需要的。它被设计为充当浏览器的人 - 允许您“点击”链接,提交表单,登录网站等。

#2


1  

You can use urllib Module to open any website and get its HTML code. but if you want to use python to receive data from your HTML Form, you should learn one of web frameworks like: Django, webpy or Flask.

您可以使用urllib模块打开任何网站并获取其HTML代码。但是如果你想使用python从你的HTML表单接收数据,你应该学习一个Web框架,如:Django,webpy或Flask。

Edit: You should replace "req_username" with "login_user", and "req_password" with "pass_word".

编辑:您应该将“req_username”替换为“login_user”,将“req_password”替换为“pass_word”。

#3


1  

I've used for this PyQt. The window has a browser widget in which it loads the web page. After that if fills needed fields (user name and password) and submits the form. I think this solution has advantage over simple form submit that it works with login pages with Javascript making changes to the page after it has loaded.

我用过这个PyQt。该窗口有一个浏览器小部件,用于加载网页。之后,如果填写所需的字段(用户名和密码)并提交表单。我认为这个解决方案优于简单的表单提交,它适用于登录页面,Javascript在加载后对页面进行了更改。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
url = "http://192.168.173.66:8080"
username = "c58941376"
password = "7758"

import sys, signal

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

def JSEval(code):
    return webpage.mainFrame().evaluateJavaScript(code)

def onLoadStarted():
    print "Loading started: %s" % webpage.mainFrame().url().toString()

def onLoadFinished(result):
    print "Loading finished: %s" % webpage.mainFrame().url().toString()
    if not result:
        #print "Request failed"
        return

    JSEval("_form = document.getElementsByName('authenticateForm')[0];")
    if JSEval("_form.tagName").toString() != "FORM" :
        print "Couldn't find needed form. Not a login page (wrong address or already logged in)."
        return
    JSEval("_form.username.value='%s';" % username \
        + "_form.password.value='%s';" % password \
        + "_form.submit();")
    print "Login data sent"


app = QApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)

webpage = QWebPage()
webpage.connect(webpage, SIGNAL("loadFinished(bool)"), onLoadFinished)
webpage.connect(webpage, SIGNAL("loadStarted()"), onLoadStarted)
webpage.mainFrame().load(QUrl(url))

web = QWebView()
web.setPage(webpage)
web.show()

sys.exit(app.exec_())