简体   繁体   English

如何在 Python 中用 selenium 刮这条线?

[英]How to scrape this line with selenium in Python?

Hey so im using selenium and im trying to scrape this line:嘿,所以我使用硒,我试图这条线:

<em id="home-payOrderCommission" data-spm-anchor-id="portals._cps_home.overview.i0.6da22fe0oBPXYk">US $7.68</em>

Im trying to scrape the $7.68 part but I cant figure out how to do it, I tried by element ID but it doesn't seem to work我试图刮掉7.68 美元的部分,但我不知道该怎么做,我按元素 ID 尝试过,但似乎不起作用

This is what I tried:这是我尝试过的:

search = driver.find_element_by_id("portals._cps_home.overview.i0.2e1b2fe03tjTTD").text
print(search)

you are using the wrong id您使用了错误的ID

use this instead :改用这个:

id = 'home-payOrderCommission'

in code :在代码中:

search = driver.find_element_by_id("home-payOrderCommission").text
print(search)

or a way better approach here is to use Explicit waits :或者这里更好的方法是使用显式等待

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
print(wait.until(EC.visibility_of_element_located((By.ID, "home-payOrderCommission"))).text)

PS :-附:-

.text is a method available in Selenium-Python bindings, basically to get the text between the ID tag in your case, and in general to extract the text of an web element. .text 是一种在 Selenium-Python 绑定中可用的方法,基本上是为了在你的情况下获取 ID 标签之间的文本,通常用于提取 web 元素的文本。

You are using a wrong locator.您使用了错误的定位器。
As can be seen from the element you presented, it has id attribute with value of home-payOrderCommission and data-spm-anchor-id attribute with value of portals._cps_home.overview.i0.6da22fe0oBPXYk .这可以从你介绍的元素可以看出,它具有id与价值属性home-payOrderCommissiondata-spm-anchor-id与价值属性portals._cps_home.overview.i0.6da22fe0oBPXYk
So, to use find_element_by_id method the value is home-payOrderCommission .因此,要使用find_element_by_id方法,该值是home-payOrderCommission
Try this:试试这个:

search = driver.find_element_by_id("home-payOrderCommission").text
print(search)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM