簡體   English   中英

(Python) 如何同時使用 selenium 和 requests 打印出一個微軟頁面

[英](Python) How to use both selenium and requests to print out a microsoft page

所以基本上我試圖在這個頁面上登錄微軟: https : //login.live.com/

在此處輸入圖片說明

使用 selenium,因為 requests 是針對更基本的身份驗證頁面。 這是我的硒代碼

email = 'password'
password = 'password'

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By


EMAILFIELD = (By.ID, "i0116")
PASSWORDFIELD = (By.ID, "i0118")
NEXTBUTTON = (By.ID, "idSIButton9")

browser = webdriver.Chrome()
browser.get('https://login.live.com')

WebDriverWait(browser, 10).until(EC.element_to_be_clickable(EMAILFIELD)).send_keys(email)

WebDriverWait(browser, 10).until(EC.element_to_be_clickable(NEXTBUTTON)).click()

WebDriverWait(browser, 10).until(EC.element_to_be_clickable(PASSWORDFIELD)).send_keys(password)

WebDriverWait(browser, 10).until(EC.element_to_be_clickable(NEXTBUTTON)).click()

WebDriverWait(browser, 10).until(EC.element_to_be_clickable(NEXTBUTTON)).click()

然后我想在我驗證后使用請求來打印頁面。 這是我的請求代碼:

from bs4 import BeautifulSoup
import requests
    
req = requests.get("https://account.microsoft.com/?lang=en-US&refd=account.live.com&refp=landing&mkt=EN-US")

soup = BeautifulSoup(req.text, 'html.parser')
print(soup.title)
print(req)

預期結果是此頁面的 html 標題: https : //account.microsoft.com/?lang=en-US&refd=account.live.com&refp=landing&mkt=EN-US (Microsoft 帳戶 | 主頁)

<title>Microsoft account | Home</title>
<Response [200]>

在此處輸入圖片說明

但相反,我得到的輸出是

<title>Microsoft account | Sign In or Create Your Account Today – Microsoft</title>
<Response [200]>

這是此頁面: https : //account.microsoft.com/account (Microsoft 帳戶 | 立即登錄或創建您的帳戶 – Microsoft)

在此處輸入圖片說明

請幫助我,並在此先感謝您!

這是因為你是通過 selenium 登錄並通過 bs抓取的,它們是相互獨立的,所以無論何時你這樣做

"https://account.microsoft.com/"

通過請求,您需要通過 API 登錄(如果有),或者簡單地說,它們都是不同的會話

JYFI,如果你這樣做

browser.get("https://account.microsoft.com/")

然后打印標題,它會起作用,但它不適用於當前的方法

暫無
暫無

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

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