如何在Python中使用Selenium获取网页的标题?
title
方法用于检索用户当前正在处理的网页的标题。它给出了驱动程序在selenium中加载的当前网页的标题,如果网页没有标题,将返回空字符串。
网页标题:页面标题,也称为标题标签,是网页的简短描述,出现在浏览器窗口的顶部和 SERP 中。它是优化 SEO 页面的重要元素。页面标题应在标题标签中包含页面的关键字。
句法 :
driver.title
争论 :
它不需要争论。
返回值:
它以字符串格式返回网页的标题。
代码 1:
# importing webdriver from selenium
from selenium import webdriver
# Here Chrome will be used
driver = webdriver.Chrome()
# URL of website
url = "https://www.geeksforgeeks.org/"
# Opening the website
driver.get(url)
# Getting current URL source code
get_title = driver.title
# Printing the title of this URL
print(get_title)
输出 :
GeeksforGeeks | A computer science portal for geeks
代码 2:
# importing webdriver from selenium
from selenium import webdriver
# Here Chrome will be used
driver = webdriver.Chrome()
# URL of website
url = "https://www.google.com/"
# Getting current URL source code
get_title = driver.title
# Printing the title of this URL
# Here it is null string
print(get_title, " ", len(get_title))
# Opening the website
driver.get(url)
# Getting current URL source code
get_title = driver.title
# Printing the title of this URL
print(get_title, " ", len(get_title))
输出 :
Google
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。