{"id":13462547,"url":"https://github.com/bupt1987/html-parser","last_synced_at":"2025-03-25T01:32:18.076Z","repository":{"id":8949828,"uuid":"10686133","full_name":"bupt1987/html-parser","owner":"bupt1987","description":"php html parser，类似与PHP Simple HTML DOM Parser，但是比它快好几倍","archived":false,"fork":false,"pushed_at":"2019-08-17T08:35:46.000Z","size":80,"stargazers_count":525,"open_issues_count":3,"forks_count":153,"subscribers_count":35,"default_branch":"master","last_synced_at":"2025-02-09T14:38:58.227Z","etag":null,"topics":["html","html-parser","parser"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bupt1987.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-06-14T09:42:01.000Z","updated_at":"2024-11-15T02:52:58.000Z","dependencies_parsed_at":"2022-09-07T06:22:03.674Z","dependency_job_id":null,"html_url":"https://github.com/bupt1987/html-parser","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bupt1987%2Fhtml-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bupt1987%2Fhtml-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bupt1987%2Fhtml-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bupt1987%2Fhtml-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bupt1987","download_url":"https://codeload.github.com/bupt1987/html-parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245382184,"owners_count":20606165,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["html","html-parser","parser"],"created_at":"2024-07-31T12:00:51.544Z","updated_at":"2025-03-25T01:32:17.853Z","avatar_url":"https://github.com/bupt1987.png","language":"PHP","funding_links":[],"categories":["Uncategorized","PHP","其他( Miscellaneous )"],"sub_categories":["Uncategorized"],"readme":"HtmlParser\n===============\n[![Total Downloads](https://img.shields.io/badge/downloads-9.4k-green.svg)](https://packagist.org/packages/bupt1987/html-parser)\n[![Build Status](https://api.travis-ci.org/bupt1987/html-parser.svg)](https://travis-ci.org/bupt1987/html-parser)  \n\nphp html解析工具，类似与PHP Simple HTML DOM Parser。\n由于基于php模块dom，所以在解析html时的效率比 PHP Simple HTML DOM Parser 快好几倍。\n\n\n注意：html代码必须是utf-8编码字符，如果不是请转成utf-8  \n      如果有乱码的问题参考：http://www.fwolf.com/blog/post/314  \n\n现在支持composer\n\n\"require\": {\"bupt1987/html-parser\": \"dev-master\"}\n\n加载composer  \nrequire 'vendor/autoload.php';\n\n================================================================================\n##### *Example*\n~~~\n\u003c?php\nrequire 'vendor/autoload.php';\n\n$html = '\u003chtml\u003e\n  \u003chead\u003e\n    \u003cmeta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /\u003e\n    \u003ctitle\u003etest\u003c/title\u003e\n  \u003c/head\u003e\n  \u003cbody\u003e\n    \u003cp class=\"test_class test_class1\"\u003ep1\u003c/p\u003e\n    \u003cp class=\"test_class test_class2\"\u003ep2\u003c/p\u003e\n    \u003cp class=\"test_class test_class3\"\u003ep3\u003c/p\u003e\n    \u003cdiv id=\"test1\"\u003e测试1\u003c/div\u003e\n  \u003c/body\u003e\n\u003c/html\u003e';\n$html_dom = new \\HtmlParser\\ParserDom($html);\n$p_array = $html_dom-\u003efind('p.test_class');\n$p1 = $html_dom-\u003efind('p.test_class1',0);\n$div = $html_dom-\u003efind('div#test1',0);\nforeach ($p_array as $p){\n\techo $p-\u003egetPlainText() . \"\\n\";\n}\necho $div-\u003egetPlainText() . \"\\n\";\necho $p1-\u003egetPlainText() . \"\\n\";\necho $p1-\u003egetAttr('class') . \"\\n\";\necho \"show html:\\n\";\necho $div-\u003einnerHtml() . \"\\n\";\necho $div-\u003eouterHtml() . \"\\n\";\n?\u003e\n~~~\n\n基础用法\n================================================================================\n~~~\n// 查找所有a标签\n$ret = $html-\u003efind('a');\n\n// 查找a标签的第一个元素\n$ret = $html-\u003efind('a', 0);\n\n// 查找a标签的倒数第一个元素\n$ret = $html-\u003efind('a', -1); \n\n// 查找所有含有id属性的div标签\n$ret = $html-\u003efind('div[id]');\n\n// 查找所有含有id属性为foo的div标签\n$ret = $html-\u003efind('div[id=foo]'); \n~~~\n\n高级用法\n================================================================================\n~~~\n// 查找所有id=foo的元素\n$ret = $html-\u003efind('#foo');\n\n// 查找所有class=foo的元素\n$ret = $html-\u003efind('.foo');\n\n// 查找所有拥有 id属性的元素\n$ret = $html-\u003efind('*[id]'); \n\n// 查找所有 anchors 和 images标记 \n$ret = $html-\u003efind('a, img'); \n\n// 查找所有有\"title\"属性的anchors and images \n$ret = $html-\u003efind('a[title], img[title]');\n~~~\n\n层级选择器\n================================================================================\n~~~\n// Find all \u003cli\u003e in \u003cul\u003e \n$es = $html-\u003efind('ul li');\n\n// Find Nested \u003cdiv\u003e tags\n$es = $html-\u003efind('div div div'); \n\n// Find all \u003ctd\u003e in \u003ctable\u003e which class=hello \n$es = $html-\u003efind('table.hello td');\n\n// Find all td tags with attribite align=center in table tags \n$es = $html-\u003efind('table td[align=center]'); \n~~~\n\n嵌套选择器\n================================================================================\n~~~\n// Find all \u003cli\u003e in \u003cul\u003e \nforeach($html-\u003efind('ul') as $ul) \n{\n       foreach($ul-\u003efind('li') as $li) \n       {\n             // do something...\n       }\n}\n\n// Find first \u003cli\u003e in first \u003cul\u003e \n$e = $html-\u003efind('ul', 0)-\u003efind('li', 0);\n~~~\n\n属性过滤\n================================================================================\n~~~\n支持属性选择器操作:\n\n过滤\t描述\n[attribute]\t匹配具有指定属性的元素.\n[!attribute]\t匹配不具有指定属性的元素。\n[attribute=value]\t匹配具有指定属性值的元素\n[attribute!=value]\t匹配不具有指定属性值的元素\n[attribute^=value]\t匹配具有指定属性值开始的元素\n[attribute$=value]\t匹配具有指定属性值结束的元素\n[attribute*=value]\t匹配具有指定属性的元素,且该属性包含了一定的值\n~~~\n\nDom扩展用法\n===============================================================================\n~~~\n获取dom通过扩展实现更多的功能，详见：http://php.net/manual/zh/book.dom.php\n\n/**\n * @var \\DOMNode\n */\n$oHtml-\u003enode\n\n$oHtml-\u003enode-\u003echildNodes\n$oHtml-\u003enode-\u003eparentNode\n$oHtml-\u003enode-\u003efirstChild\n$oHtml-\u003enode-\u003elastChild\n等等...\n\n~~~\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbupt1987%2Fhtml-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbupt1987%2Fhtml-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbupt1987%2Fhtml-parser/lists"}