我希望能够从div内部获得2个链接.
目前我可以选择一个,但是它有更多似乎不起作用.
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(url);
HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[@class='myclass']");
if (node != null)
{
foreach (HtmlNode type in node.SelectNodes("//a@href"))
{
recipe.type += type.InnerText;
}
}
else
recipe.type = "Error fetching type.";
Run Code Online (Sandbox Code Playgroud)
试图从这段HTML中获取它:
<div class="myclass">
<h3>Not Relevant Header</h3>
<a href="#">This text</a>,
<a href="#">and this text</a>
</div>
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏,在此先感谢.
现在我有点进退两难。我正在尝试使用更多 Laravel 约定来优化旧代码,而不是构建自己的循环来获取我需要的数据。
这就是我现在所拥有的:
区域模型
class Region extends Eloquent {
public $timestamps = false;
public function vms() {
return $this->hasMany('Vm');
}
}
Run Code Online (Sandbox Code Playgroud)
游戏模型
class Game extends Eloquent {
public $table = 'vm_games';
public function vm() {
return $this->hasOne('Vm');
}
}
Run Code Online (Sandbox Code Playgroud)
虚拟机型号
class Vm extends Eloquent {
public function games() {
return $this->hasMany('Game');
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想要做的事情如下:
$games = 0;
foreach($region->vms()->where('status', true)->get() as $vm) {
$games += $vm->games()->where('created_at', '>', date('Y-m-d H:i:s', time() - 5400))->count();
}
$sorted[$region->id]->games = $games;
Run Code Online (Sandbox Code Playgroud)
当然,现在使用 Laravels Eloquent 关系更加清晰: …