What is the safest way to extract <title> from an HTML file using xpath?

Here is my current xpath code "/html/head/title".

But you know that in the real world environment html the code format is usually interrupted, for example. The tag is <html>missing, it may cause an exception. So, I would like to know if there is a safe way to extract the tag <title>? (something like getElementByTagName)

+3
source share
5 answers

"//title" perhaps?

+4
source

Due to the naughty nature of html markup, you should use the html parsing library. You did not specify a platform or language, but there are a number of open source libraries there.

+3
source

/html/head/title , , :

  • title;
  • HTML , ;
  • HTML- HTML .

HTML, /html/head/title[1], , .

+2

javascript, :

document.title
+1

-, XML ( HTML, XPath), //title .

0

Source: https://habr.com/ru/post/1760219/


All Articles