【问题标题】:Using selenium I want to scrape this text使用硒我想刮掉这段文字
【发布时间】:2020-04-15 13:22:19
【问题描述】:

大家好,我正在尝试使用 selenium 从this url 抓取以下文本: 但我收到错误,它无法通过该类名找到任何元素。这是我的代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

driver = webdriver.Chrome(r'C:\Users\User\AppData\Local\Programs\Python\Python37\Lib\site-packages\chromedriver_py\chromedriver_win32.exe')
driver.get('https://xangle.io/project/XTZ/full-disclosure')
driver.find_element_by_class_name('fv1').text

【问题讨论】:

  • 请分享html

标签: python selenium xpath web-scraping webdriverwait


【解决方案1】:

诱导WebDriverWait() 和visibility_of_element_located() 并跟随xpath。

driver.get("https://xangle.io/project/XTZ/full-disclosure")
print(WebDriverWait(driver,15).until(EC.visibility_of_element_located((By.XPATH,"//div[text()='Token Name']/following-sibling::div[1]"))).text)

print(WebDriverWait(driver,15).until(EC.visibility_of_element_located((By.XPATH,"(//div[@class='token-profile-cont']//div[@class='fv1'])[1]"))).text)

导入以下库。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

【讨论】:

  • 它有效但它给了我错误的元素..它给了我“公司简介”但我想刮掉“Tezos”这个词..我在屏幕截图中圈出了它..你能帮我吗.. 并感谢您修复错误
  • @SabbirTalukdar :我认为它需要登录才能获取 html?请您发布元素的 html。以便我可以给您确切的解决方案。
  • 我已经分享了这个网址,如果你能访问它,那对你会有很大的帮助。也许这样你会看到整个图片
  • Tezos
  • 成功了,谢谢.. 你发布的第二个对我来说更容易理解,谢谢
【解决方案2】:

错误是因为有多个具有相同类名的元素。

使用 XPATH 更好地遍历页面识别所需的元素。

WebDriverWait(driver,15).until(EC.visibility_of_element_located((By.XPATH,"//div[contains(@class, 'token-name')]/div[2]")))

driver.find_element_by_xpath("//div[contains(@class, 'token-name')]/div[2]").text

这将为您提供令牌名称文本内容。

【讨论】:

  • NoSuchElementException: 消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//div[contains(@class, 'token-name') ]/div[2]"}(会话信息:chrome=80.0.3987.163)
  • 尝试使用 webdriver 等待。
猜你喜欢
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 2021-06-27
  • 2020-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-04
相关资源
最近更新 更多