有没有一种方法可以从calendarjs(也许)中提取数据?python beautifulsou selenium post日历刮削

2024-06-07 07:39:38 发布

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

我想从这个网站的日历中提取数据。 https://www.dreamplus.asia/event/list

如果我单击EVNET或日历中事件日期的标记。标签的详细信息会在日历的右侧弹出。正如你所见,这个网站是由js(可能)(如果你看到详细的页面来源)

即使我用selenium来点击日期或事件的标签,我也找不到如何点击这些东西。有什么帮助吗?你知道吗

    # -*- coding: utf-8 -*- 
    import os
    import re
    import json
    import requests
    from bs4 import BeautifulSoup
    import traceback
    from pprint import pprint 
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options

    def dreamplus():    
        options = Options()
        driver = webdriver.Chrome(executable_path='../../chromedriver.exe',         options=options)

        driver.get("https://www.dreamplus.asia/event/list")

        #driver = launchBrowser()
        html = driver.page_source
        soup = BeautifulSoup(html, 'html.parser')
        #Days = driver.find_elements_by_xpath("//*        [@id='calendar']/div[@class='fc-view-container']/div[@class='fc-view fc-month-view fc-basic-view']/table/tbody[@class='fc-body']/tr/td[@class='fc-widget-content']/div[@class='fc-scroller fc-day-grid-container']/div/div/div/table")
        Controllers = driver.find_elements_by_class_name('fc-event-container')
        print(Controllers)
        for list in Controllers:
            print(list.text)

        driver.close()



    if __name__ == '__main__':
        try:
            dreamplus()
        except BaseException as e:
                    with open('dreamplus_error.log','wt') as f:
                            f.write(traceback.format_exc())
                            f.close()

我使用find \u elements \u by \u class \u name来获取“fc event container”以获取项目,但“Controllers”为空。可能是因为它是js。。你知道吗


Tags: fromimportdiveventviewcontainerdriverselenium
1条回答
网友
1楼 · 发布于 2024-06-07 07:39:38

我注意到,如果您试图直接转到event,您将被重新定向到主页。因此,您可以转到home包并单击事件,或者只需在一行中执行两个.get。注意:您需要容器中的子a标记,以便单击以更新侧边栏信息。你知道吗

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

d = webdriver.Chrome()
d.get('https://www.dreamplus.asia/')
d.get('https://www.dreamplus.asia/event/list')
events = WebDriverWait(d,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".fc-event-container a")))
events[2].click()  #example event click

点击(较慢):

d.get('https://www.dreamplus.asia/')
event_tabs = d.find_elements_by_xpath("//*[contains(text(), 'Event')]")
event_tabs[0].click()
event_tabs[1].click()
events = WebDriverWait(d,20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".fc-event-container a")))
events[2].click()  #example event click

相关问题 更多 >

    热门问题