如何使用Python(BeautifulSoap,请求)刮取使用BankID登录的网站?

2024-04-24 06:07:45 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用Python(BeautifulSoap,请求)刮取使用BankID登录的网站

我想用BankID登录,然后用python抓取一个网站。如何登录到使用BankID登录的网站

常规方法不起作用:

from bs4 import BeautifulSoup

import requests

source = requests.get('https://example.com').text

soup = BeautifulSoup(source, 'lxml')

print(soup)

Tags: 方法fromhttpsimportsourceget网站example
1条回答
网友
1楼 · 发布于 2024-04-24 06:07:45

最好的方法是使用selenium。以下是您如何使用selenium:

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

driver = webdriver.Chrome() #Opens the chromedriver. If u don't have a chromedriver, u can download it from https://chromedriver.chromium.org/downloads 

driver.get('Url of the banking website') #Opens the specified url

bank_id = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, 'Textbox xpath'))) #Finds the bank_id textbox 

bank_id.click() #Clicks it

bank_id.send_keys('Your username') #Enters ur username in the textbox
 
pswrd = driver.find_element_by_xpath('Xpath of textbox') #Does the same for the password.

pswrd.click()

pswrd.send_keys('Your password')

login_btn = driver.find_element_by_xpath('Xpath of submit button') #Finds the login button

login_btn.click() #Clicks the login button

就这样!您已登录该网站!希望这有帮助

相关问题 更多 >