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 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#的问题,如何解决?