解析(抓取)网页时如何将"display: flex"更改为"display: none"? |Python (telegram-bot) |Selenium



下午好。我正在开发一个电报机器人,它可以解析(抓取(公共来源的信息。语言:Python

在这种特殊的情况下,你需要制作一个简单的网站截图,并将其发送给用户。网站链接:https://deepstatemap.live/

程序上一切都完成了,但有一个问题,当访问网站时,在第一次确认之前会出现免责声明,即:

<div class="disclaimer" style="display: flex;">...</div>

相应地,出现了免责声明的屏幕截图。

你能告诉我如何用替换html布局吗

<div class="disclaimer" style="display: none;">...</div>

为了获得我需要的确切材料。

代码片段:

import telebot
from telebot import types
import requests
from bs4 import BeautifulSoup
from config import TOKEN, URL6
import time
from selenium import webdriver
from PIL import Image
import os
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('--disable-extensions')
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')
@bot.message_handler(content_types=['text'])
def text(message):
if message.chat.type == 'private':
if message.text == 'MAP':
url6 = URL6
photo_path6 = str(message.chat.id) + '.png'
driver = webdriver.Chrome(options=options)
driver.set_window_size(1140, 1140)
driver.get(url6)
time.sleep(3)
driver.save_screenshot(photo_path6)

bot.send_photo(chat_id=message.chat.id,
photo=open(photo_path6, 'rb'),
reply_markup=markup10, parse_mode='html')

driver.quit()
os.remove(photo_path6)

负责免责声明的网页片段:

<div class="disclaimer" style="display: flex;">
<div class="inner">
<div class="inner-text">
<h2>УВАГА!</h2>
<p>
Мапу <strong>небезпечно та заборонено</strong> використовувати для прокладання маршрутів евакуації, вона має неточності та оновлюється з затримкою
</p>
</div>
<div class="inner-buttons">
<button class="disagree">НЕ<br>ПОГОДЖУЮСЬ</button>
<button class="agree">ПОГОДЖУЮСЬ</button>
</div>
<center><img alt="DeepStateMap Logo" src="images/meta_og.jpg"></center>
</div>
</div>

到目前为止的结果:在此处输入图像描述

您可以将div捕获为WebElement,然后将其传递给driver.execute_script()以将样式属性设置为display: none;

这个代码应该适用于你:

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(executable_path='D://chromedriver/100/chromedriver.exe')
wait = WebDriverWait(driver, 20)
url = "https://deepstatemap.live/"
driver.get(url)
# capturing the disclaimer div
disclaimer = wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="disclaimer"]')))
# setting the style attribute to display: none;
driver.execute_script("arguments[0].setAttribute('style', 'display: none;')", disclaimer)