dongming4994 2010-05-04 18:41
浏览 136
已采纳

从html文档中提取特定部分,php cURL,php,preg_match

I'm trying to extract some information from a webpage using php cURL+preg_match or any other function but for some reasons it doesn't work at all . For example from this page, I want to extract the title which is "4 bed house to rent, Caroline Place, Bayswater, W2", the price which is "2,300" and the description which starts at "This fantastic..." and ends at "(Circle and District Lines). ". I tried to use php cURL + dom but I'm getting a lot of errors like this "htmlParseEntityRef: expecting ';' in Entity, line: 243" and no result displayed

Also I tried to use preg_match or preg_match_all but doesn't work either .

A very basic example would be highly appreciated !

  • 写回答

4条回答 默认 最新

  • dpqy77560 2010-05-04 18:57
    关注

    A very basic example would be highly appreciated

    To answer the regex part:

    preg_match('!<title>(.*)</title>!s', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
    <title>
    
                4 bedroom
    
    
            house
    
    
        to rent in Caroline Place, Bayswater, W2 through Foxtons (Property to rent)</title>
    <meta name="keywords" content="Houses" />', $matches);
    print_r($matches);
    
    /* output:
    Array
    (
        [0] => <title>
    
                4 bedroom
    
    
            house
    
    
        to rent in Caroline Place, Bayswater, W2 through Foxtons (Property to rent)</title>
        [1] => 
    
                4 bedroom
    
    
            house
    
    
        to rent in Caroline Place, Bayswater, W2 through Foxtons (Property to rent)
    )
    */
    

    The s at the end of the regex puts the parser into something (inaptly) called single-line mode.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办