如何使用curl登录网页?

如何使用curl登录网页?
Testing site: http://testing-ground.scraping.pro/login
Username: admin
Password: 12345

卷曲帮助

-u, --user <user:password> Server user and password

这是网络表格

wolf@linux:~$ curl http://testing-ground.scraping.pro/login 2>/dev/null | sed -n '/<form/,/form>/'p
    <form action="login?mode=login" method="POST">
        <label for="usr">User name:</label>
        <input id="usr" name="usr" type="text" placeholder="enter 'admin' here">
        <label for="pwd">Password:</label>
        <input id="pwd" name="pwd" type="text" placeholder="enter '12345' here">
        <input type="submit" value="Login">
    </form>
wolf@linux:~$ 

没有凭据的 Curl

wolf@linux:~$ curl http://testing-ground.scraping.pro/login?mode=login 2>/dev/null | egrep 'DENIED|WELCOME'
        <li>If you see <span class="success">WELCOME :)</span> then the user credentials were sent, the cookie was passed and HTTP redirect was processed</li>
        <li>If you see <span class="error">ACCESS DENIED!</span> then either you entered wrong credentials or they were not sent to the server properly</li>
<h3 class='error'>ACCESS DENIED!</h3><a href='login'>&lt;&lt;&nbsp;GO BACK</a></div>
wolf@linux:~$ 

然而,带有凭证的curl-u admin:12345也显示相同的页面......

wolf@linux:~$ curl http://testing-ground.scraping.pro/login?mode=login -u admin:12345 2>/dev/null | egrep 'DENIED|WELCOME'
        <li>If you see <span class="success">WELCOME :)</span> then the user credentials were sent, the cookie was passed and HTTP redirect was processed</li>
        <li>If you see <span class="error">ACCESS DENIED!</span> then either you entered wrong credentials or they were not sent to the server properly</li>
<h3 class='error'>ACCESS DENIED!</h3><a href='login'>&lt;&lt;&nbsp;GO BACK</a></div>
wolf@linux:~$ 

在这种情况下使用curl的正确方法是什么?

答案1

这似乎有效:

curl -s -L --cookie-jar cookies.txt -d 'usr=admin&pwd=12345' http://testing-ground.scraping.pro/login?mode=login | grep -E 'DENIED|WELCOME'

输出:

        <li>If you see <span class="success">WELCOME :)</span> then the user credentials were sent, the cookie was passed and HTTP redirect was processed</li>
        <li>If you see <span class="error">ACCESS DENIED!</span> then either you entered wrong credentials or they were not sent to the server properly</li>
<h3 class='success'>WELCOME :)</h3><a href='login'>&lt;&lt;&nbsp;GO BACK</a></div>
  • -s安静模式,无进度条。输出发送到标准输出。
  • -L遵循 302 重定向
  • --cookie-jar cookies.txt使用cookie并将cookie写入文件cookies.txt
  • -d 'usr=admin&pwd=12345'发布数据usr=adminpwd=12345

相关内容