需要帮助从网页中抓取特定的跨度标签

时间:2021-03-07 15:12:04

标签: html selenium web-scraping beautifulsoup

这里是html代码:

<button data-v-63a32c95="" disabled="disabled" class="bt-button bt-default" data-v-1b41da19=""><!----> <span>Sold Out</span> <!----></button>

我正在尝试从跨度标签中删除“售罄”。我已经试过这个当前的代码

wd2 = webdriver.Chrome('chromedriver',options=options)
wd2.get("https://shop.bitmain.com/product/detail?pid=00020210224195530399kqcF32sc06B9")
b = wd2.find_elements_by_xpath("//*[@class='bt-button bt-default']//span") 
#b = wd2.find_elements_by_xpath("//span[@class='bt-button bt-default']")
for i in b:
    print(i.text)

当我运行它时,它没有返回任何东西。我尝试了其他漂亮的汤方法,例如尝试选择页面上的所有跨度标签,但仍然没有运气。我对网络抓取还很陌生,希望得到一些帮助和见解!

2 个答案:

答案 0 :(得分:1)

您的 xpath 选择器是正确的,但您需要等到页面呈现完毕,使用 WebDriverWait

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

.....

# wait 10 second until element rendered
button = WebDriverWait(driver, 10).until(
             EC.presence_of_element_located((By.XPATH, '//*[@class="bt-button bt-default"]//span')))
print(button.text)

答案 1 :(得分:0)

元素位于 selenium webdriver 元素中,您需要先从中提取 HTML,然后才能检索其文本。

driver_element..get_attribute("outerHTML")

试试这个:

wd2 = webdriver.Chrome('chromedriver',options=options)
wd2.get("https://shop.bitmain.com/product/detail?pid=00020210224195530399kqcF32sc06B9")
b = wd2.find_elements_by_xpath("//*[@class='bt-button bt-default']//span") 
#b = wd2.find_elements_by_xpath("//span[@class='bt-button bt-default']")
for i in b:
    text = i.get_attribute("outerHTML")
    print(text)

相关问题