简体   繁体   English

如何使用 Selenium 和 Python 在 html 代码中提取特定文本

[英]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.

相关问题 PYTHON + SELENIUM(铬):如何从当前URL中提取特定文本,然后使用提取的文本转到其他文本? - PYTHON + SELENIUM (CHROME): How can I extract a specific text from my current url and use the extracted text to go to another? 如何使用python从HTML代码中提取特定元素 - How can I extract a specific element from HTML code using python 在 python/selenium 中,如何在 HTML 代码中获得竞争 - In python/selenium, how can I get the contend in HTML code 如何使用 Python 和 BeautifulSoup 在网站的 HTML 代码中找到特定文本? - How can I find specific text in a website's HTML code with Python and BeautifulSoup? 如何使用 Selenium 和 Python 从 HTML 中提取文本 - How to extract the text from the HTML using Selenium and Python 如何使用 Selenium 和 Python 从 html 中提取文本 H MATTHEWS - How to extract the text H MATTHEWS from the html using Selenium and Python 如何使用Selenium和Python遍历项目列表并提取特定部分 - How can I iterate through a list of items and extract a specific part using Selenium and Python 如何在Python中使用html.parser从特定的HTML链接中提取数据? - How can I extract data from a specific HTML link with html.parser in Python? 如何在 Python 中使用 BeautifulSoup 从 html 中提取特定文本? - How to extract specific text from html using BeautifulSoup in Python? 如何修复Python 3代码以从文本文件中提取特定行 - How to fix Python 3 code to extract specific lines from a text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM