douzong3599 2016-11-24 12:02
浏览 6

刮标题标签的内容

I want to fetch the content of the heading tag given below:

<h1 style="BACKGROUND-COLOR: white; line-height: 2em; margin:0 .5em .2em .5em; padding: 4px 8px 4px 8px; border-radius: 10px;-moz-border-radius: 10px; -webkit-border-radius: 10px; border: 1px solid silver;text-decoration:none; font-size: 2.1em;">Seminar Report</h1><div style='position:relative; visibility:visible; width:100%; overflow:auto;' align='center'>

Can I do it using file_get_html() or file_get_contents()?

This is what i am getting when I use var_dump($html);

object(simple_html_dom)#1 (23) { ["root"]=> object(simple_html_dom_node)#2 (9) { ["nodetype"]=> int(5) ["tag"]=> string(4) "root" ["attr"]=> array(0) { } ["children"]=> array(2) { [0]=> object(simple_html_dom_node)#3 (9) { ["nodetype"]=> int(2) ["tag"]=> string(7) "comment" ["attr"]=> array(0) { } ["children"]=> array(0) { } ["nodes"]=> array(0) { } ["parent"]=> RECURSION ["_"]=> array(2)

and so on

and when I use var_dump($html->find('h1'));I get

array(1) { [0]=> object(simple_html_dom_node)#48 (9) { ["nodetype"]=> int(1) ["tag"]=> string(2) "h1" ["attr"]=> array(1) { ["style"]=> string(233) "BACKGROUND-COLOR: white; line-height: 2em; margin:0 .5em .2em .5em; padding: 4px 8px 4px 8px; border-radius: 10px;-moz-border-radius: 10px; -webkit-border-radius: 10px; border: 1px solid silver;text-decoration:none; font-size: 2.1em;" } ["children"]=> array(0) { } ["nodes"]=> array(1) { [0]=> object(simple_html_dom_node)#49 (9) { ["nodetype"]=> int(3) ["tag"]=> string(4) "text" ["attr"]=> array(0) { } ["children"]=> array(0) { } ["nodes"]=> array(0) { } ["parent"]=> *RECURSION* ["_"]=> array(1) { [4]=> string(14) "Seminar Report" } ["tag_start"]=> int(0) ["dom":"simple_html_dom_node":private]=> object(simple_html_dom)#1 (23) { ["root"]=> object(simple_html_dom_node)#2 (9) { ["nodetype"]=> int(5) ["tag"]=> string(4) "root" ["attr"]=> array(0) { } ["children"]=> array(2) { [0]=> object(simple_html_dom_node)#3 (9) { ["nodetype"]=> int(2) ["tag"]=> string(7) "comment" ["attr"]=> array(0) { } ["children"]=> array(0) { } ["nodes"]=> array(0) { } ["parent"]=> *RECURSION* ["_"]=> array(2) { [0]=> int(1) [4]=> string(23) "" } 
  • 写回答

1条回答 默认 最新

  • douguai4653 2016-11-24 12:04
    关注

    I believe it will be easy to use file_get_html($url):

    $html = file_get_html('http://www.google.com/');
    echo $html->find('h1')->plaintext;
    

    Notes

    • Make sure you have SimpleHTMLDom Library added.
    • The above code uses http://www.google.com/ as sample. You should replace it with your URL.
    • The above code gets only the first <h1>'s text output.
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?