Selenium:抓取和打印 类
Selenium: scraping and print classes
这是我第一次使用 Selenium。我之前在 Whosebug 上看过其他问题,但它们对我的帮助不大。我想从网站 https://link 上“仅”打印文本 Giornata 38 和 Giornata 37。没有内容,只有这两个文本用于说明目的。我指定一切都是为了教育、学习和形成目的,仅供我自己学习 Python.
我做错了什么? Python代码或html现场检查?重要提示:如果我写错了代码,你修改/改进它,记得留在 Firefox,不要用 Chrome 替换它。谢谢
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Firefox()
browser.get('link')
giornata = browser.find_element_by_class_name("event__round event__round--static")
print(giornata)
browser.quit()
display.stop()
错误:引发 exception_class(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.NoSuchElementException:消息:无法定位元素:.event__round event__round--static
class event__round event__round--static
包含一个 space " "
,你必须删除这个 space 并用点连接 class .
.
而不是:
giornata = browser.find_element_by_class_name("event__round event__round--static")
尝试:
giornata = browser.find_element_by_class_name("event__round.event__round--static")
print(giornata.text)
根据您的评论,如果您想访问特定元素,例如“Giornata 37”和“Giornata 38”,您可以使用 :nth-of-type(n)
CSS select 或, div.event__round:nth-of-type(2)
将 select 一个 div
然后第二个 class event__round
.
在你的例子中:
print(browser.find_element_by_css_selector("div.event__round:nth-of-type(15)").text)
print(browser.find_element_by_css_selector("div.event__round:nth-of-type(2)").text)
输出:
Giornata 37
Giornata 38
这是我第一次使用 Selenium。我之前在 Whosebug 上看过其他问题,但它们对我的帮助不大。我想从网站 https://link 上“仅”打印文本 Giornata 38 和 Giornata 37。没有内容,只有这两个文本用于说明目的。我指定一切都是为了教育、学习和形成目的,仅供我自己学习 Python.
我做错了什么? Python代码或html现场检查?重要提示:如果我写错了代码,你修改/改进它,记得留在 Firefox,不要用 Chrome 替换它。谢谢
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Firefox()
browser.get('link')
giornata = browser.find_element_by_class_name("event__round event__round--static")
print(giornata)
browser.quit()
display.stop()
错误:引发 exception_class(消息、屏幕、堆栈跟踪) selenium.common.exceptions.NoSuchElementException:消息:无法定位元素:.event__round event__round--static
class event__round event__round--static
包含一个 space " "
,你必须删除这个 space 并用点连接 class .
.
而不是:
giornata = browser.find_element_by_class_name("event__round event__round--static")
尝试:
giornata = browser.find_element_by_class_name("event__round.event__round--static")
print(giornata.text)
根据您的评论,如果您想访问特定元素,例如“Giornata 37”和“Giornata 38”,您可以使用 :nth-of-type(n)
CSS select 或, div.event__round:nth-of-type(2)
将 select 一个 div
然后第二个 class event__round
.
在你的例子中:
print(browser.find_element_by_css_selector("div.event__round:nth-of-type(15)").text)
print(browser.find_element_by_css_selector("div.event__round:nth-of-type(2)").text)
输出:
Giornata 37
Giornata 38