简体   繁体   中英

How do I extract all HTML tags from a webpage into an array?

I need to extract all HTML tags from a webpage into an array without the data inside the tags . It would look something like...

I'm using PHP

Array 
{
   html =>
             Array 
             {
                 head =>
                          Array
                          {
                              title,
                              meta name='description' content='bla bla'
                              meta name='keyword' content='bla bla'
                              ....
                          },
                 body =>
                          Array
                          {
                              div id='header' =>
                                              Array
                                              {
                                                  div class='logo',
                                                  div class='nav'
                                              },
                              div id='content' =>
                                              Array
                                              {
                                                  h1,
                                                  p class='first-para',
                                                  p,
                                                  p,
                                                  div id='ad'
                                              },
                              div id='footer' =>
                                              Array
                                              {
                                                  ul =>
                                                        Array
                                                        {
                                                            li =>
                                                                  Array
                                                                  {
                                                                     a href='link.htm'
                                                                  },
                                                            li =>
                                                                  Array
                                                                  {
                                                                     a href='link.htm'
                                                                  },
                                                            li =>
                                                                  Array
                                                                  {
                                                                     a href='link.htm'
                                                                  }
                                                        }
                                              }
                          }

             }
}

What you need is an HTML parser (an XML parser would probably not do because HTML often is invalid). Maybe: http://simplehtmldom.sourceforge.net/

您还可以使用PHP DOM扩展

I think the simplest way is to use XPath.

//*::name()

Should give you the names of all nodes on all levels. Iam not sure wheather not hierarchy will be flattened though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM