English 中文(简体)
灰 Python:请求可登录网站
原标题:Python: requests can t login to a website

我需要拆除需要登录的网站。 I m试图创建session和标志,因为在伐木之后,我不得不拆解不同的页面。 但是,它为什么不工作。

import requests
from bs4 import BeautifulSoup

login_data = {
           "log":"login",
           "login":"my email",
           "password":"my password"
}

session = requests.session()
session.post(login_url, data=login_data)
response = session.get(url)
html = response.text
soup = BeautifulSoup(html, "html.parser")
print(soup.title.get_text())

标题表明它没有工作。

这里是网站表格。

<form method="post" id="signin-form" class="form-horizontal">
    <input type="hidden" name="referer" value="" />
    <div class="form-group">
        <label for="email_text" class="col-sm-4 control-label">Your login (email):</label>
        <div class="col-sm-8">
            <input type="email" class="form-control" id="email_text" value="" name="login" autofocus data-validation= {"parent":".form-group","events":["keyup","blur"],"rules":[{"name":"notblank"},{"name":"email"}]}  />
        </div>
    </div>
    <div class="form-group">
        <label for="password_text" class="col-sm-4 control-label">Password:</label>
        <div class="col-sm-8">
            <input type="password" class="form-control" id="password_text" name="password" data-validation= {"parent":".form-group","rules":[{"name":"min","min":5}]}  />
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-8 col-sm-offset-4">
            <div class="checkbox">
                <label>
                    <input type="checkbox" name="rememberme"> Remember me on this computer
                </label>
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-4 col-sm-8">
            <button type="submit" class="btn btn-default btn-lg" name="log">Log into your account</button>
            <a class="btn btn-default btn-lg mobile-show-inline-block" href="/account/create/">Create account</a>
            <a href="/account/lostpassword" class="btn btn-link btn-lg">Forgot your password?</a>
        </div>
    </div>
</form>

N.B: Don t 建议我使用selenium。 我可以通过<代码>selenium <>/code>做到这一点,我测试了但我必须贴上requests是因为selenium。 即使在我使用<代码>时,也出现ole。 PhantomJS。

最佳回答

提炼在原木首页。 也许它会设立一些厨师,希望能到这个职位上来。

问题回答

I know that this question was made long ago, but anyway, I ll propose a solution for those who are still having trouble with this: I recommend to check if the form you are trying to post takes some kind of hidden input, which the example of the question does. This is very frequent, and does sometimes prevent us from logging to a site if we do not notice it. So, let s suppose in the site there is a form like this:

<form method= post  id= signin-form  class= big-form >
 <input type="hidden" id="whatever" name="foo" value="check">
 <input type="text" id="u" name="user">
 <input type="password" id="pwd" name="pass">
</form>

在这种情况下,可变的<代码>login_data 如同:

login_data = {
       "foo":"check",
       "user":"your username",
       "pass":"your password",           
}

在这样做之后,如果网站没有检查负责人,你就不应通过请求模块将麻烦记录到网站。

各位可能会失踪。 我将拦截一个网络浏览器提出的请求,看看你失踪的情况,然后在你的要求中添加这些标题。

您将了解如何在正式文件上这样做:https://requests.readthedocs.io/en/latest/user/quickstart.html#custom-headers





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签