2016-08-26 89 views
0

我知道這個問題看起來很直截了當,但我已經嘗試了所有建議,但都沒有成功。颳去需要驗證的網站

我想建立一個Python腳本來檢查我的學校網站,看看是否已經放好了新的成績。然而,我不能爲了我的生活找出如何刮掉它。

該網站重定向到不同的頁面進行登錄。我嘗試了所有可以找到的腳本和答案,但是我迷路了。

我使用Python 3,該網站是一個https://blah.schooldomate.state.edu.country/website/grades/summary.aspx 格式

的用戶名部分包含以下內容:

<input class="txt" id="username" name="username" type="text" autocomplete="off" style="cursor: auto;">

的密碼是名稱不同在於它含有一個onfocus HTML元素。

一個已成功驗證,我自動重定向到正確的頁面。

我曾嘗試:

使用Python 2的cookielib和機械化

使用HTTPBasicAuth

傳遞的信息作爲字典的requests.get()

嘗試了許多不同的民族代碼包括我在本網站上找到的答案

+0

您可以驗證?如果是這樣,你必須遵循使用python請求的重定向並使用會話來存儲cookie –

+0

最簡單的方法是使用chrome登錄,並從開發工具獲取cURL url,並使用它進行操作。 – YOU

回答

0

也許你可以使用Selenium庫。

我讓你我的代碼示例:

from selenium import webdriver 

def loging(): 
    browser = webdriver.Firefox() 
    browser.get("www.your_url.com") 

    #Edit the XPATH of Loging INPUT username 
    xpath_username = "//input[@class='username']" 

    #Edit the XPATH of Loging INPUT password 
    xpath_password = "//input[@class='password']" 

    #THIS will write the YOUR_USERNAME/pass in the xpath (Custom function) 
    click_xpath(browser, xpath_username, "YOUR_USERNAME") 
    click_xpath(browser, xpath_username, "YOUR_PASSWORD") 

    #THEN SCRAPE WHAT YOU NEED 

#Here is the custom function 
#If NO input, will only click on the element (on a button for example) 
def click_xpath(self, browser, xpath, input="", time_wait=10): 
    try: 
     browser.implicitly_wait(time_wait) 
     wait = WebDriverWait(browser, time_wait) 
     search = wait.until(EC.element_to_be_clickable((By.XPATH, xpath))) 
     search.click() 
     sleep(1) 
     #Write in the element 
     if input: 
      search.send_keys(str(input) + Keys.RETURN) 
     return search 
    except Exception as e: 
     #print("ERROR-click_xpath: "+xpath) 
     return False