2014-09-30 35 views
0

我想回复一些类似于这个问题的其他网站的数据Getting data from another site with php via ID获取一个页面的表格数据

表中有一行我想获取并回显,但无法使其回显任何内容。

这是我的代码,因为我调整它来自​​上述问题的代码,但它不起作用。

$content = "http://voucher.gov.gr/project/pedy-results/gid/14?search=PDNO-78256-114-20140722-120951"; 

$ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, $content);   
    curl_setopt($ch, CURLOPT_NOBODY, false); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    $body= curl_exec ($ch); 
    curl_close ($ch); 

    preg_match('#<tr class="row0"><td>([0-9\.]*)</td><td>([0-9\.]*)</td><td>([0-9\.]*)</td><td>([0-9\.]*)</td><td>([0-9\.]*)</td><td>([0-9\.]*)</td><td>([0-9\.]*)</td><td>([0-9\.]*)</td><td>([0-9\.]*)</td><td>([0-9\.]*)</td><td>([0-9\.]*)</td>#Uis', $body, $resultmatch); 

    $results = $resultmatch; 

    foreach($results as $word) 
    echo $word; 

该数组虽然创建但没有数据。任何帮助/建议,将不胜感激谢谢!

编辑 解答:谢谢大家的帮助,但我设法使它工作!这是代码:

preg_match('#<td>(.*)</td>(.*)<td>(.*)</td>(.*)<td style="max-width:151px;"><strong>(.*)</strong></td>(.*)<td>(.*)</td>(.*)<td>(.*)</td>(.*)<td>(.*)</td>(.*)<td>(.*)</td>(.*)<td>(.*)</td>(.*)<td>(.*)</td>(.*)<td>(.*)</td>#Uis', $body, $resultmatch); 

此代码是不是绝对正确的答案,因为这不仅会返回内的信息对TD的我想,它也返回它们之间的白色空间,这就是因为代码不能没有工作在td之间放置“(。*)”。因为我不得不忍受它!但是,您可以通过忽略带有空格的结果插入数组(在我们的例子中为resultmatch[2,4,6,8,10...]等等)来避免它。我希望我的编辑帮助。当然可以进一步改进代码以避免将空白插入到数组中。

+1

你调试的过程中的每一步?例如,你有什么东西返回到'$ body'吗? – Raad 2014-09-30 09:27:14

+0

@Raad yeap body正确返回整个页面,我认为问题存在于preg_match内,但我对表达式不熟悉。 – fotis179 2014-09-30 09:36:08

+0

如果我是正确的(RegExp不是我的强项之一),那么匹配查找包含11个表格单元格的css类“row0”的行,而这些表格单元格只包含数字。所寻址的页面在前5个单元格中包含非数字内容,因此不匹配。 – Raad 2014-09-30 09:48:29

回答

0

确认卷曲正确返回页面正文后,您的问题正确,因为preg_match

匹配查找包含11个表格单元的css类“row0”的行,其中每个表格都包含以数字开头的内容,后面跟着任何内容([0-9\.]*)

页解决在第一5个细胞是在启动非数字,因此没有比赛的内容,因此如果要匹配这一行,你可以改变表达式:

'#<tr class="row0"><td>(.*)</td><td>(.*)</td><td>(.*)</td><td>(.*)</td><td>(.*)</td><td>([0-9\.]*)</td><td>([0-9\.]*)</td><td>([0-9\.]*)</td><td>([0-9\.]*)</td><td>([0-9\.]*)</td><td>([0-9\.]*)</td>#Uis' 

正如我所说的在我的评论中,正则表达式并不是我强大的技能之一(因此我的评论中它有点不对),所以虽然我认为这会起作用,但您可能需要调整它。

我觉得RegExp“小提琴”网站http://regex101.com/确实有用。

+0

你的答案真的帮我做到了!谢谢! – fotis179 2014-10-01 07:44:12

+0

@ fotis179 - 如果您认为我的答案有窍门,请点击大白色的勾号=) – Raad 2014-10-01 08:14:35

0

如果您检查$ body,则会出现大量不必要的空白和换行符,这些空白和换行符会阻止您的表达式找到匹配项。

为了字母数字字符串匹配你需要的东西像“(。*?)\ U”

音符结束前U,允许符合Unicode字符。

所以我觉得这是你所需要的:

$content = "http://voucher.gov.gr/project/pedy-results/gid/14?search=PDNO-78256-114-20140722-120951"; 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $content);   
curl_setopt($ch, CURLOPT_NOBODY, false); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
$body= curl_exec ($ch); 
curl_close ($ch); 

//you need to strip whitespace and line breaks first 
$body = preg_replace('~>[\s|\r\n]+<~', '><', $body); 
$body = preg_replace('#\n(*?)#', '', $body); 
preg_match('#<tr class=\"row0\"><td>(.*?)</td><td>(.*?)</td><td(.*?)>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td>#u', $body, $resultmatch); 
var_dump($resultmatch); 

上述结果是这样的:

array (size=13) 
    0 => string '<...>' (length=398) 
    1 => string 'Στερεάς Ελλάδας' (length=29) 
    2 => string 'Φθιώτιδας' (length=18) 
    3 => string ' style="max-width:151px;"' (length=25) 
    4 => string '<strong>PDNO-78256-114-20140722-120951</strong>' (length=47) 
    5 => string ' 
      22/07/2014         12:09:51        ' (length=99) 
    6 => string 'Επιλεχθείς' (length=20) 
    7 => string '30' (length=2) 
    8 => string '30' (length=2) 
    9 => string '30' (length=2) 
    10 => string '10' (length=2) 
    11 => string '100' (length=3) 
    12 => string '1        ' (length=33) 
+0

真的很好的答案在这里剥离白色空间。当我找到时间的时候会试试看!谢谢! – fotis179 2014-10-01 07:42:49

0

我相信你不应该使用正则表达式来解析HTML元素。

使用DOM API将不太容易出错。

你可以替换使用“的preg_match”行:

libxml_use_internal_errors(true); 
$domDocument = new DOMDocument(); 
$domDocument->loadHTML($body); 
$xpath = new DOMXPath($domDocument); 
$nodes = $xpath->query('//tr[@class="row0"][1]/td'); 

$results = array(); 
foreach($nodes as $node) { 
    $value = trim($node->nodeValue); 
    if(ctype_digit($value)) { 
     $results[] = $node->nodeValue; 
    } 
} 
相关问题