【问题标题】:curl scraping a single website two levels deepcurl 抓取单个网站两级深度
【发布时间】:2020-01-04 17:23:10
【问题描述】:

我需要抓取我(房地产)客户的旧网站,以便将数据保存在我创建的新网站中。
使用 curl。
深两层。索引页面,然后是属性详细信息页面。
在索引页面中,我需要 curl 来获取页数,因此我的脚本的下一部分可以深入研究所有这些页面并获取每个属性的所有属性数据。

在第一个函数中(parseURL)我需要获取页数

/* This function does the initial parsing to get the number of pages */
public function parseURL($url) {
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $curl_scraped_page = curl_exec($ch);
  // bring back all the html of the page
  // echo "csp=$curl_scraped_page";
  $data = str_replace(array("\n", "\r"), "", preg_replace('/(?:(?<=\>)|(?<=\/\>))(\s+)(?=\<\/?)/', "", $curl_scraped_page));
  curl_close($ch);
  $regex = '#<div style="float:right;width:540px"><h3 style="margin-top:0px">(.*)</h3><h4>(.*)</h4>(.*)<div style="padding:5px"><a href="(.*)">(.*)</a></div></div>#siU';
  // $regex = '#<div class="propertyListLinks"><a href="(.*)">(.*)</a></div#siU';
  preg_match_all($regex, $data, $this->details);
  $regex2 = '#[[0-9]{1,4}]#';
  // echo "<br />data=\n$data<br />";
  preg_match_all($regex2, $data, $this->pagination);
  // exit;
}

很久以前为我写的,我不记得正则表达式在做什么,我想了解这一点,以便我可以开发它以满足我当前的需求。

请给我建议:

  1. #$regex$regex2 字符串中做了什么?
  2. siU$regex 字符串末尾的含义是什么?

【问题讨论】:

    标签: php curl web-scraping


    【解决方案1】:

    1) # 是一个 REGEX 模式分隔符 - 即表示模式的开始和结束。在 PHP 使用的 REGEX 的 PCRE 风格下,哈希是 one of several 分隔符。

    2) 这些是flags,它告诉模式在某些方面如何表现。在你的情况下:

    • s 表示模式应将点 (.) 视为任何字符的别名,包括换行符。
    • i 表示模式应该忽略大小写
    • U 表示任何 repeaters 都应该以不贪婪的方式匹配

    Full reference

    【讨论】:

    • 非常感谢 :) 我似乎无法找到可以“投票”您的回复的地方。 :\
    • 赞成票在答案的左上角完成(见两个箭头)。如果您认为这是解决方案,请接受答案。
    猜你喜欢
    • 1970-01-01
    • 2015-09-14
    • 2015-03-30
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    相关资源
    最近更新 更多