我正在尝试找出我应该使用的正则表达式,以便从gov.uk网站获取一些数据。
基本上,我在以下网址上使用了file_get_contents:
作为一个例子 - +城堡+学校取代[学校名称]。
这会返回4个结果。我希望能够获取返回的所有结果的学校ID,学校名称和学校地址。可能有多页结果,因此抓取所有结果非常重要。
我一直在尝试使用RegExBuddy来做这件事,但我无法让它发挥作用。
关于每个结果返回的数据相当一致如下: -
<li class="document">
<div>
<h3>
<a class="bold-small" href="/school/110182">The Castle School</a>
</h3>
<div class="comparsion-button-container">
<div id="JsAddRemoveError" class="optional-section no-js-hidden">
<p class="error-message">An error had occurred whilst trying to add or remove this school or college to comparison. Try again now or later.</p>
</div>
<a class="button button-comparison button-comparison-add" id="AddComparison110182" href="/addCompare/110182/searchResults/find-a-school-in-england?keywords=The+Castle+School&suggestionurn=&searchtype=search-by-name"
data-js-url="/add-to-comparison-js/110182/searchResults">Add <span class="visuallyhidden">The Castle School </span>to comparison list</a>
</div>
</div>
<dl class="metadata">
<dt>Address<span aria-hidden="true">:</span></dt>
<dd>Love Lane, Newbury, RG14 2JG</dd>
<dt class="visuallyhidden">Phase of education<span aria-hidden="true">:</span></dt>
<dd>Primary, Secondary and 16 to 18</dd>
<dt>School type<span aria-hidden="true">:</span></dt>
<dd>Special School</dd>
<dt>Ofsted rating<span aria-hidden="true">:</span></dt>
<dd>
<span class="rating rating-1" aria-hidden="true">
<span class="rating-text">
1
</span>
</span>
Outstanding
<span class="rating-date">
<span><span aria-hidden="true">(</span>Last inspection<span aria-hidden="true">:</span></span>
<span>
<time datetime="2014-10-08">08 October 2014</time><span aria-hidden="true">)</span>
</span>
</span>
</dd>
</dl>
<div style="clear: both;"></div>
每个结果都封装在
中<li class=document">
并在此处找到学校名称和学校ID: -
<a class="bold-small" href="/school/110182">The Castle School</a>
在这种情况下,学校ID为110182,学校名称为The Castle School。
地址也始终夹在: -
<dd>Love Lane, Newbury, RG14 2JG</dd>
对于返回超过1页结果的结果集的示例,您可以使用“Grammar”一词
我意识到这是一个很大的问题,但我一直在尝试使用RegExBuddy尝试创建正确的正则表达式,但似乎无法找到正确的答案。
如果您对如何获取所需信息有更好的了解,请告诉我们。我知道他们提供下载的数据,但我不想这样做,因为它会涉及存储数据并不断更新 - 而他们网站上的数据将始终是最新的。
感谢。
编辑:通过我的评论查看Jan的回答。非常令人印象深刻
答案 0 :(得分:2)
与往常一样,使用解析和正则表达式的组合:
<?php
$url = 'https://www.compare-school-performance.service.gov.uk/?keywords=[SCHOOL-NAME]&suggestionurn=&searchtype=search-by-name';
$previous_value = libxml_use_internal_errors(TRUE);
$dom = new DOMDocument();
$dom->loadHTMLFile($url);
$xpath = new DOMXPath($dom);
# regex part
$regex = '~(?P<id>\d+)$~';
# here comes the main part
$schools = $xpath->query("//ul[@class = 'school-results-listing']//li");
foreach($schools as $school) {
$name = $xpath->query(".//h3/a/text()", $school)->item(0)->nodeValue;
preg_match($regex, $xpath->query(".//h3/a/@href", $school)->item(0)->nodeValue, $match);
$id = $match["id"];
$address = $xpath->query(".//dl[@class = 'metadata']//dd/text()", $school)->item(0)->nodeValue;
echo "Name: {$name}, ID: {$id}, Address: {$address} \n";
}
libxml_clear_errors();
libxml_use_internal_errors($previous_value);
?>
这会将文档加载到DOM
中,遍历它并借助于id部分的简单正则表达式提取所需信息。
不要直接在HTML
上使用正则表达式。