簡體   English   中英

請求自動登錄的Python Web抓取不起作用

[英]Python Web scraping with Requests Automated Login Not Working

我一直在嘗試使用python requests模塊通過網絡抓取網站,並且需要登錄該網站以檢索所需的數據。 我到處環顧四周,但找不到原因。 到目前為止,這是我的代碼:

import requests
import bs4 as bs

login_url = "__withheld__"
target_url = "__withheld__"

login_data = { "username": "my_username", "password": "my_password"}

with requests.Session() as s:
    page = s.get(login_url)
    page_login = s.post(login_url, data = login_data)
    page = s.get(target_url)
    final_page = bs.BeautifulSoup(page.content, 'lxml')
    print(final_page.title)

這是密碼框的html:

<input name="username" type="text" id="username" class="metro-input" placeholder="Username" value="">
<span id="username-error" class=""></span>
<label class="ie789Only"> Password</label>
<input name="password" type="password" id="password" class="metro-input" placeholder="Password">
<input type="submit" name="button1" value="Sign in" id="button1" class="metro-button">

我認為這可能與要求用戶單擊按鈕的網站有關,盡管我找不到任何解決方案。 當我登錄自己時,我還嘗試在開發人員控制台中查找任何張貼表格,但沒有找到明確的表格來概述密碼/用戶名。 任何幫助表示贊賞。

更新如果有幫助,以下是指向具有相同安全功能的同一公司(隱私)運營的網站的鏈接: https : //ashwood-vic.compass.education/login.aspx?sessionstate=disabled

您可以一次嘗試以下代碼嗎

import requests
import bs4 as bs
username = 'username of the site'
password = 'password of the site'

req = requests.get(login_url, auth=(username, password))
final_page = bs.BeautifulSoup(req.content, 'lxml')
print(final_page.title)

-請參閱此http://docs.python-requests.org/en/master/user/authentication/#basic-authentication

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM