Selenium/BeautifulSoup - WebScrape 该字段

2024-05-25

我的代码运行良好,并打印除带有下拉列表的行之外的所有行的标题。

例如,如果单击第 4 行,则会出现一个下拉菜单。我实现了一个“尝试”,理论上会单击下拉菜单,然后拉出标题。

但是,当我执行 click() 并尝试打印时,对于具有这些下拉列表的行,它们不会打印。

预期输出 - 打印所有标题,包括下拉列表中的标题。

用户已在此链接上提交了答案StackOverFlow 答案 https://stackoverflow.com/questions/68473642/selenium-webscrape-this-field#68474005但他的答案的格式不同,我不知道如何用他的方法添加日期、时间、椅子等字段或顶部的“按需”字段

任何方法都将受到赞赏,希望将其放入数据框中。谢谢

from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome()
actions = ActionChains(driver)

driver.get('https://cslide.ctimeetingtech.com/esmo2021/attendee/confcal/session/list')
time.sleep(4)
page_source = driver.page_source
soup = BeautifulSoup(page_source,'html.parser')

new_titles = set()

productlist=driver.find_elements_by_xpath("//div[@class='card item-container session']")
for property in productlist:
    actions.move_to_element_with_offset(property,0,0).perform()
    time.sleep(4.5)
    sessiontitle=property.find_element_by_xpath(".//h4[@class='session-title card-title']").text
    #print(sessiontitle)
    ifDropdown=property.find_elements_by_xpath(".//*[@class='item-expand-action expand']")
    if(ifDropdown):
        ifDropdown[0].click()
        time.sleep(4)
        open_titles = driver.find_elements_by_class_name('card-title')
        for open_title in open_titles:
            title = open_title.text
            if(title not in new_titles):
                print(title)
                new_titles.add(title)

你的问题是driver.find_elements_by_class_name('item-expand-action expand')命令。这find_elements_by_class_name('item-expand-action expand')定位器错误。这些网络元素有多个类名。要定位这些元素,您可以使用 css_selector 或 XPath。
此外,由于有多个带有下拉菜单的元素,要对它们执行单击,您应该迭代它们。你不能执行.click()在网络元素列表上。
所以你的代码应该是这样的:

ifDropdown=driver.find_elements_by_css_selector('.item-expand-action.expand')
for drop_down in ifDropdown:
    drop_down.click()
    time.sleep(0.5)

除了上面的 css_selector 之外,您还可以使用 XPath:

ifDropdown=driver.find_elements_by_xpath('//a[@class="item-expand-action expand"]')

UPD
如果您想打印添加的新标题,您可以执行以下操作:

ifDropdown=driver.find_elements_by_css_selector('.item-expand-action.expand')
for drop_down in ifDropdown:
    drop_down.click()
    time.sleep(0.5)
newTitles=driver.find_elements_by_class_name('card-title')
for new_title in newTitles:
    print(new_title.text)

在展开所有下拉元素之后,我将获得所有新标题,然后迭代该列表打印每个元素文本。
driver.find_elements_by_class_name返回网络元素的列表。您不能申请.text在列表上,您必须迭代列表元素,每次都获取每个元素文本。
UPD2
打开下拉菜单并打印其内部标题的整个代码可以是这样的:
我用 Selenium 来做这个,而不是与 bs4 混合。

from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.action_chains import ActionChains

import time
driver = webdriver.Chrome()
actions = ActionChains(driver)

driver.get('https://cslide.ctimeetingtech.com/esmo2021/attendee/confcal/session/list')
time.sleep(4)
page_source = driver.page_source
soup = BeautifulSoup(page_source,'html.parser')

new_titles = set()

productlist=driver.find_elements_by_xpath("//div[@class='card item-container session']")
for property in productlist:
    actions.move_to_element(property).perform()
    time.sleep(0.5)
    sessiontitle=property.find_element_by_xpath(".//h4[@class='session-title card-title']").text
    print(sessiontitle)
    ifDropdown=property.find_elements_by_xpath(".//*[@class='item-expand-action expand']")
    if(ifDropdown):
        ifDropdown[0].click()
        time.sleep(4)
        open_titles = driver.find_elements_by_class_name('card-title')
        for open_title in open_titles:
            title = open_title.text
            if(title not in new_titles):
                print(title)
                new_titles.add(title)

我在这里检查是否有下拉菜单。如果是的话,我打开它。然后获取当前所有已打开的标题。对于每个这样的标题,我都会验证它是否是新的或之前打开过。如果标题是新的,不存在于集合中,我将打印它并将其添加到集合中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Selenium/BeautifulSoup - WebScrape 该字段 的相关文章

随机推荐