(Python) 如何同时使用 selenium 和 requests 打印出微软页面

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

所以基本上我是在这个页面上尝试登录微软:https://login.live.com/

使用 selenium,因为请求用于更基本的身份验证页面。这是我的硒代码

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(如果有)登录,或者简单地说,它们都是不同的 session

JYFI,如果你这样做

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

然后打印标题,它会起作用,但它不适用于当前的方法