Selenium 使用 python 提取网络响应

问题描述 投票:0回答:1

所以基本上我正在尝试从我的大学网站中提取出勤率,并且我正在尝试使用selenium自动登录到我已经成功完成的网站,并且在自动单击到菜单后我想提取包含html的网络数据包的响应包含所需信息的代码。

enter image description herefrom selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import requests
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from browsermobproxy import Server
from seleniumwire import webdriver
from seleniumwire.utils import decode

username = "VU21CSEN0100120"
password = "password@123"
driver = webdriver.Chrome()
driver.get("https://newgstudent.gitam.edu/Home/GetAttendancedatachart?category=subject")
username_textbox = driver.find_element(By.ID, "txtusername")
username_textbox.send_keys(username)

password_textbox = driver.find_element(By.ID,"password")
password_textbox.send_keys(password)

login_button = driver.find_element(By.ID,"Submit")
login_button.click()

button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CLASS_NAME, 'Attendance'))
)
button.click()
time.sleep(5)
for requests in driver.requests:
if requests.url == "https://newgstudent.gitam.edu/Home/GetAttendancedatachart?category=subject":
body=print(requests.body)

我尝试过 requests.body 但我得到了一些随机编码的文本

python selenium-webdriver web-scraping networking packet-capture
1个回答
0
投票

您的代码中似乎有一个小错误。您应该使用

for requests in driver.requests
,而不是
for request in driver.requests
。 此更正后的代码应打印您感兴趣的网络请求的 HTML 内容。

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

username = "VU21CSEN0100120"
password = "password@123"

# Set up the browser with seleniumwire
driver = wiredriver.Chrome()

# Open the login page
driver.get("https://newgstudent.gitam.edu/Home/GetAttendancedatachart?category=subject")

# Fill in the login form
username_textbox = driver.find_element(By.ID, "txtusername")
username_textbox.send_keys(username)

password_textbox = driver.find_element(By.ID, "password")
password_textbox.send_keys(password)

login_button = driver.find_element(By.ID, "Submit")
login_button.click()

# Wait for the page to load and the desired element to be clickable
button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.CLASS_NAME, 'Attendance'))
)
button.click()

# Wait for a short period to ensure the network requests are captured
time.sleep(5)

# Loop through the captured requests and find the one with the desired URL
for request in driver.requests:
    if request.url == "https://newgstudent.gitam.edu/Home/GetAttendancedatachart?category=subject":
        # Print the body of the request (HTML content)
        print(request.response.body)

# Close the browser
driver.quit()

© www.soinside.com 2019 - 2024. All rights reserved.