【问题标题】:preg_match_all How to get all links?preg_match_all 如何获取所有链接?
【发布时间】:2015-12-06 13:16:41
【问题描述】:

我正在尝试从我正在抓取的页面中获取所有带有 preg_match_all 的图像链接,这些链接以 http://i.ebayimg.com/ 开头并以 .jpg 结尾。我无法正确执行... :( 我试过这个但这不是我需要的……:

preg_match_all('/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/', $contentas, $img_link);

正常链接也有同样的问题...我不知道如何将 preg_match_all 写入此:

<a class="link--muted" href="http://suchen.mobile.de/fahrzeuge/details.html?id=218930381&daysAfterCreation=7&isSearchRequest=true&withImage=true&scopeId=C&categories=Limousine&damageUnrepaired=NO_DAMAGE_UNREPAIRED&zipcode=&fuels=DIESEL&ambitCountry=DE&maxPrice=11000&minFirstRegistrationDate=2006-01-01&makeModelVariant1.makeId=3500&makeModelVariant1.modelId=20&pageNumber=1" data-touch="hover" data-touch-wrapper=".cBox-body--resultitem">

非常感谢!!!

更新 我正在尝试从这里: http://suchen.mobile.de/fahrzeuge/search.html?isSearchRequest=true&amp;scopeId=C&amp;makeModelVariant1.makeId=1900&amp;makeModelVariant1.modelId=10&amp;makeModelVariant1.modelDescription=&amp;makeModelVariantExclusions%5B0%5D.makeId=&amp;categories=Limousine&amp;minSeats=&amp;maxSeats=&amp;doorCount=&amp;minFirstRegistrationDate=2006-01-01&amp;maxFirstRegistrationDate=&amp;minMileage=&amp;maxMileage=&amp;minPrice=&amp;maxPrice=11000&amp;minPowerAsArray=&amp;maxPowerAsArray=&amp;maxPowerAsArray=PS&amp;minPowerAsArray=PS&amp;fuels=DIESEL&amp;minCubicCapacity=&amp;maxCubicCapacity=&amp;ambitCountry=DE&amp;zipcode=&amp;q=&amp;climatisation=&amp;airbag=&amp;daysAfterCreation=7&amp;withImage=true&amp;adLimitation=&amp;export=&amp;vatable=&amp;maxConsumptionCombined=&amp;emissionClass=&amp;emissionsSticker=&amp;damageUnrepaired=NO_DAMAGE_UNREPAIRED&amp;numberOfPreviousOwners=&amp;minHu=&amp;usedCarSeals= 获取汽车链接和图片链接以及所有信息,有信息就一切正常,我的脚本运行良好,但我在抓取图片和链接时遇到问题.. 这是我的脚本:

<?php

        $id= $_GET['id'];
        $user= $_GET['user'];
        $login=$_COOKIE['login'];

    $query = mysql_query("SELECT pavadinimas,nuoroda,kuras,data,data_new from mobile where vartotojas='$user' and id='$id'");
    $rezultatas=mysql_fetch_row($query);

    $url = "$rezultatas[1]";

    $info = file_get_contents($url); 

function scrape_between($data, $start, $end){
$data = stristr($data, $start); 
$data = substr($data, strlen($start));
$stop = stripos($data, $end);
$data = substr($data, 0, $stop);
return $data;
  }
     //turinio iskirpimas
    $turinys = scrape_between($info, '<div class="g-col-9">', '<footer class="footer">');
     //filtravimas naikinami mokami top skelbimai
    $contentas = preg_replace('/<div class="cBox-body cBox-body--topResultitem".*?>(.*?)<\/div>/', '' ,$turinys);
    //filtravimas baigtas

      preg_match_all('/<span class="h3".*?>(.*?)<\/span>/',$contentas,$pavadinimas); 

      preg_match_all('/<span class="u-block u-pad-top-9 rbt-onlineSince".*?>(.*?)<\/span>/',$contentas,$data); 

      preg_match_all('/<span class="u-block u-pad-top-9".*?>(.*?)<\/span>/',$contentas,$miestas);

      preg_match_all('/<span class="h3 u-block".*?>(.*?)<\/span>/', $contentas, $kaina);

      preg_match_all('/<a[A-z0-9-_:="\.\/ ]+href="(http:\/\/suchen.mobile.de\/fahrzeuge\/[^"]*)"[A-z0-9-_:="\.\/ ]\s*>\s*<div/s', $contentas, $matches);

   print_r($pavadinimas);
   print_r($data);
   print_r($miestas);
   print_r($kaina);
   print_r($result);
   print_r($matches);

   ?>

【问题讨论】:

  • 您能否提供输入和所需的输出?因为不是很清楚
  • 还是不清楚。请在脚本末尾添加您想要的内容,而不是添加您的代码。你想要一个包含 url 的所有 a 和 img 标签的数组:suchen.mobile.de/.....jpg?你想要它们在 2 个数组还是 2 个单独的数组中?
  • 是的,我想要所有 a href 的数组,而不是 img src 的所有数组。例如: $all_images = preg_match_all("这里选择所有包含 url i.ebayimg.com/HERE 的图像链接总是不同的文本/109.JPG"); $all_ahref = preg_match_all("这里是所有以suchen.mobile.de/fahrzeuge/" 开头的ahref 链接); 然后我做for() 并将所有内容放在表中!非常感谢!!!

标签: php regex web-scraping preg-match-all


【解决方案1】:

1. 捕获所有img 标签中以http://i.ebayimg.com/ 开头的src 属性:

正则表达式:/src=\"((?:http|https):\\/\\/i.ebayimg.com\\/.+?.jpg)\"/i

这是一个例子:

$re = "/src=\"((?:http|https):\\/\\/i.ebayimg.com\\/.+?.jpg)\"/i"; 
$str = "codeOfHTMLPage"; 
preg_match_all($re, $str, $matches);

现场查看:here

如果您想确保在img 标记上捕获此网址,请使用此正则表达式(请记住,如果页面很长,性能会降低):

$re = "/<img(?:.*?)src=\"((?:http|https):\\/\\/i.ebayimg.com\\/.+?.jpg)\"/i";

2. 捕获所有http://i.ebayimg.com/ 开头的href 属性a 标签:

正则表达式:/href=\"((?:http|https):\\/\\/suchen.mobile.de\\/fahrzeuge\\/.+?.jpg)\"/i

这是一个例子:

$re = "/href=\"((?:http|https):\\/\\/suchen.mobile.de\\/fahrzeuge\\/.+?.jpg)\"/i; 
$str = "codeOfHTMLPage"; 
preg_match_all($re, $str, $matches);

现场查看:here

如果您想确保在 a 标记上捕获此 url,请使用此正则表达式(请记住,如果页面很长,性能会降低):

$re = "/<a(?:.*?)href=\"((?:http|https):\\/\\/suchen.mobile.de\\/fahrzeuge\\/.+?.jpg)\"/i";

【讨论】:

  • 1.这不起作用。 2. 我需要获取
  • 谢谢.. 但为什么
【解决方案2】:

使用 DOMDocument 更方便:

libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTMLFile($yourURL);

$imgNodes = $dom->getElementsByTagName('img');

$result = [];

foreach ($imgNodes as $imgNode) {
    $src = $imgNode->getAttribute('src');
    $urlElts = parse_url($src);
    $ext = strtolower(array_pop(explode('.', $urlElts['path'])));
    if ($ext == 'jpg' && $urlElts['host'] == 'i.ebayimg.com')
        $result[] = $src;
}

print_r($result);

要获得“正常”链接,请使用相同的方式(DOMDocument + parse_url)。

【讨论】:

  • 1.这不起作用。 2. 我需要获取
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
  • 2019-09-27
  • 2014-03-17
  • 2021-11-26
  • 2015-06-15
相关资源
最近更新 更多