如何在网站上抓取受覆盖层隐藏的产品名称?

2024-04-26 13:22:08 发布

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

我正在尝试获取https://www.bell.ca/Mobility/Smartphones_and_mobile_internet_devices上所有设备的名称。然而,当我尝试用一个小工具抓取CSS选择器时,似乎有一个覆盖层不允许您选择带有名称的头。你知道吗

我尝试过各种各样的XPath,但是当试图获取属性下的所有名称时,它们似乎只返回名字或者根本不返回名字。我也试过了

attribute_value = WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="div_product_list_item_div_product_list_item_0"]/div[1]/div[2]/a/div[2]/span[1]')))

并进行项目迭代,但范围可能会发生变化。你知道吗

def phoneList():


    driver.get("https://www.bell.ca/Mobility/Smartphones_and_mobile_internet_devices")

    attribute_value = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="productListWithFilters"]/div[3]'))).\
        get_attribute("class='rsx-product-name hidden-xs hidden-sm selectorgadget_suggested'")


    print(attribute_value)

我希望抓取所有的设备名称,并将它们放入一个列表中,无论大小。你知道吗


Tags: andhttpsdiv名称valuewwwdriverattribute
1条回答
网友
1楼 · 发布于 2024-04-26 13:22:08

我在获取您使用的数据的所有元素时遇到问题,但请尝试以下方法:

使用find \u elements \u by \u css \u选择器查找所有设备名称元素。循环浏览这些内容,抓取文本并添加到列表中。你知道吗

list = []

cssSelector = '#productListWithFilters .rsx-product-list-product-name-group-bottom span:nth-child(1)'

devices = driver.find_elements_by_css_selector(cssSelector)

for i in devices:

     list.append(i.text)

相关问题 更多 >