How can i get attribute from xpath?

Can you use Xpath to get meta descriptions?

  • Trying to grab a website's meta description using xpath but having trouble. Is it possible, if so, what would be the proper expression to do it?

  • Answer:

    Yes, you can. Here's a PHP snippet from a working application (https://bitbucket.org/trmanco/phirce/src/5394ec29a6d1/Class.phIRCe.php): if($xpath){ $descElement = $xpath->query('/html/head/meta[@name="description"]'); $screenNameElement = $xpath->query('/html/head/meta[@name="page-user-screen_name"]'); if ($descElement->length && $screenNameElement->length){ $result['text'] = $descElement->item(0)->getAttribute('content'); $result['screen_name'] = $screenNameElement->item(0)->getAttribute('content'); } //...

Toby Thain at Quora Visit the source

Was this solution helpful to you?

Other answers

yes we can ! I am using XPath Helper Extension in Chrome (https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl) The query would be //meta[@name='description']/@content Try this on Yahoo Home Page !

Abhishesh Sharma

You can get it using Google Docs. Like so: Create a new spreadsheet in Google Docs In Cell B1 type this path =importxml(A1,"//meta[@name='description']/@content") In cell A1, type the URL of the website whose meta description you want. I got the path from here http://stackoverflow.com/questions/6052472/xpath-google-docs-importxml but I've tested it so I know it works. Note: If it returns #N/A it's probably because the page doesn't have a meta description. If it returns #VALUE, there's something wrong with the syntax.

Chavi Gourarie

The last answer (, Parselmouth) works for me with xidel - a great tool to parse xml and html in bash. You might need to check the single quotes, though. I use double quotes in this place for use in this tool: xidel temp.file -e '//meta[@name="description"]/@content'`.

Andreas W Voss

It's very simple as the previous person suggested but in my experience it requires a webpage that validates as XML. In theory that's all XHTML webpages but in practice it's only about 5% of pages that claim to be XHTML actually validate.

Chris Gilbert

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.