php blog 抓取匹配xml

jlokys 2010-07-20 04:34:24
如何从抓取的页面中匹配下面的内容
<link rel="alternate" type="application/rss+xml" href="http://blog.sina.com.cn/rss/lilianjie.xml" title="RSS" />

同时取出 href 的内容 一定要匹配 rel值 alternate type的值 application/rss+xml
...全文
90 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
leaffall 2010-07-21
  • 打赏
  • 举报
回复
最简单的办法是使用 simplehtmldom http://simplehtmldom.sourceforge.net/


// example of how to use basic selector to retrieve HTML contents
include('../simple_html_dom.php');
$string = <<<content
<link rel="alternate" type="application/rss+xml" href="http://blog.sina.com.cn/rss/lilianjie.xml" title="RSS" />
<ink rel="stylesheet" type="text/css" href="http://simg.sinajs.cn/blog7style/css/blog/newblog2.css" />
<br>
<div></div>
<lin REL="stylesheet" href="http://simg.sinajs.cn/blog7style/css/blog/newblog3.css" type="text/css"/>
<link Rel="Alternate" type="text/css" href="http://simg.sinajs.cn/blog7style/css/blog/newblog4.css" />
<link type="application/rss+xml" href="http://blog.sina.com.cn/rss/lilianjie24.xml" title="RSS" rel="alternate"/>
<link rel="Alternate" type="application/rss+xml" href="http://blog.sina.com.cn/rss/lilianjie22.xml" title="RSS" />
<link rel="alternate" href=http://blog.sina.com.cn/rss/lilianjie21.xml title="RSS" type='application/rss+xml'/>
<ink rel="stylesheet" type="text/css" href="http://simg.sinajs.cn/blog7style/css/blog/newblog5.css" />
<br>
<div></div>
<lin rel="stylesheet" type="text/css" href="http://simg.sinajs.cn/blog7style/css/blog/newblog6.css" />
content;

$html = str_get_html($string);

// find all link
foreach($html->find('link') as $e) {
if(strtolower($e->rel)=="alternate" && strtolower($e->type)=="application/rss+xml")
echo $e->href . '<br>';
}

amani11 2010-07-20
  • 打赏
  • 举报
回复
路过,看老大的代码留名
xuzuning 2010-07-20
  • 打赏
  • 举报
回复
不要在一棵树上吊死!
$string = <<<content
<link rel="alternate" type="application/rss+xml" href="http://blog.sina.com.cn/rss/lilianjie.xml" title="RSS" />
<link rel="stylesheet" type="text/css" href="http://simg.sinajs.cn/blog7style/css/blog/newblog.css" />
content;

$p = array('/<link\s+/i', '/" +/', '#/>#', '/"/', '/=/', "/[\r\n]+/");
$r = array('', '&', '', '', '[]=', '');

$s = preg_replace($p, $r, $string);

parse_str($s, $info);
print_r($info);

Array
(
[rel] => Array
(
[0] => alternate
[1] => stylesheet
)

[type] => Array
(
[0] => application/rss xml
[1] => text/css
)

[href] => Array
(
[0] => http://blog.sina.com.cn/rss/lilianjie.xml
[1] => http://simg.sinajs.cn/blog7style/css/blog/newblog.css
)

[title] => Array
(
[0] => RSS
)

)
jlokys 2010-07-20
  • 打赏
  • 举报
回复
请求帮助
jlokys 2010-07-20
  • 打赏
  • 举报
回复
$string = <<<content
<link rel="alternate" type="application/rss+xml" href="http://blog.sina.com.cn/rss/lilianjie.xml" title="RSS" />
<link rel="stylesheet" type="text/css" href="http://simg.sinajs.cn/blog7style/css/blog/newblog.css" />
content;

如果 要进行匹配的内容是 string , 我只想匹配 rel=alternate type=application/rss+xml 的标签 同时取得
href的值
CunningBoy 2010-07-20
  • 打赏
  • 举报
回复
$string = <<<content
<link rel="alternate" type="application/rss+xml" href="http://blog.sina.com.cn/rss/lilianjie.xml" title="RSS" />
<<<
preg_match_all('/<link rel=\"(.*)\" type=\"(.*)\" href=\"(.*)\"[^>]* \/>/is', $string, $matches);
print_r($matches);
床上等您 2010-07-20
  • 打赏
  • 举报
回复

//未经测试。
preg_match_all('/<link\s+rel="alternate"\s+type="application/rss+xml".*\/>/i',$strimg,$matches);
var_dump($matches);

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧