[英]How can I extract a specific text in an html code with Selenium and Python
<time class="_1o9PC Nzb55" datetime="2020-06-07T17:45:25.000Z" title="7. Juni 2020">Vor 1 Stunde</time>
I am currently web scraping with selenium.我目前正在用 selenium 刮擦 web。 The code you see is the html element of when a picture got posted on instagram.您看到的代码是在 Instagram 上发布图片时的 html 元素。 I want the code to just print this:我希望代码只打印这个:
datetime="2020-06-07T17:45:25.000Z"
Say I found the element by class and do print(element.text)
.假设我通过 class 找到了元素并执行print(element.text)
。 Then it outputs this: "Vor 1 Stunde" (sorry for being in german).然后它输出这个:“Vor 1 Stunde”(抱歉是德语)。 I don't know if there even is a way to do this but if there is, please let me know.我不知道是否有办法做到这一点,但如果有,请告诉我。 This is the whole code:这是整个代码:
from selenium import webdriver
import time, pyautogui, random
browser = webdriver.Firefox()
browser.get('https://www.instagram.com/')
time.sleep(1)
name = browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[2]/div/label/input")
name.click()
name.send_keys("username")
passwort = browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[3]/div/label/input")
passwort.send_keys("password")
browser.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div[4]/button/div").click()
time.sleep(3)
browser.find_element_by_xpath("/html/body/div[1]/section/main/div/div/div/div/button").click()
time.sleep(2)
browser.find_element_by_xpath("/html/body/div[4]/div/div/div[3]/button[2]").click()
time.sleep(2)
suche = browser.find_element_by_class_name("LWmhU").click()
time.sleep(1)
pyautogui.typewrite("mmd")
pyautogui.typewrite(["enter"])
time.sleep(2.5)
acc = browser.find_element_by_xpath("/html/body/div[1]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a[1]/div/div[2]/span").click()
print(acc)
time.sleep(1)
# click on the instagram picture
pyautogui.click(427, 754)
time.sleep(2)
uploaddate = browser.find_element_by_class_name("_1o9PC")
print(uploaddate.getAttribute("datetime"))
The desired element is a ReactJS enabled element so to locate the element you need to induce WebDriverWait for the visibility_of_element_located()
and you can use either of the following Locator Strategies :所需的元素是启用ReactJS的元素,以便定位您需要为visibility_of_element_located()
诱导WebDriverWait的元素,您可以使用以下任一定位器策略:
Using XPATH
:使用XPATH
:
print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//time[text()='Vor 1 Stunde']"))).get_attribute("datetime"))
Using CSS_SELECTOR
:使用CSS_SELECTOR
:
print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "time[title$='Juni 2020'][datetime]"))).get_attribute("datetime"))
Note : You have to add the following imports:注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.