【问题标题】:Need help scraping specific span tag from webpage需要帮助从网页中抓取特定的 span 标签
【发布时间】:2021-06-05 15:16:07
【问题描述】:

所以这里是html代码:

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

我正在尝试从 span 标记中抓取“Sold Out”。我试过这个当前的代码

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)

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

【问题讨论】:

    标签: html selenium web-scraping beautifulsoup


    【解决方案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)
    

    【讨论】:

      【解决方案2】:

      元素在 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)
      
      

      【讨论】:

      • 感谢您的帮助!谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      相关资源
      最近更新 更多