我是一名初级程序员,我制作了一个相当简单的抓取网站并将信息私下存储在 mysql 数据库中,以了解有关编程的更多信息。
这是我要抓取的代码:
<li id="liIngredient" data-ingredientid="3914" data-grams="907.2">
<label>
<span class="checkbox-formatted"><input id="cbxIngredient" type="checkbox" name="ctl00$CenterColumnPlaceHolder$recipeTest$recipe$ingredients$rptIngredientsCol1$ctl01$cbxIngredient" /></span>
<p class="fl-ing" itemprop="ingredients">
<span id="lblIngAmount" class="ingredient-amount">2 pounds</span>
<span id="lblIngName" class="ingredient-name">ground beef chuck</span>
</p>
</label>
</li>
<li id="liIngredient" data-ingredientid="5838" data-grams="454">
<label>
<span class="checkbox-formatted"><input id="cbxIngredient" type="checkbox" name="ctl00$CenterColumnPlaceHolder$recipeTest$recipe$ingredients$rptIngredientsCol1$ctl02$cbxIngredient" /></span>
<p class="fl-ing" itemprop="ingredients">
<span id="lblIngAmount" class="ingredient-amount">1 pound</span>
<span id="lblIngName" class="ingredient-name">bulk Italian sausage</span>
</p>
</label>
</li>
抓取数据后,我尝试使用 str_replace 去除除(使用第一个示例)2 磅碎牛肉(或第二个示例中的 1 磅散装意大利香肠)以外的所有内容。
这是我的尝试:
$ingredients = str_replace('#<label>\s<span class="checkbox-formatted"><input id="cbxIngredient" type="checkbox" name=".*?" /></span>\s<p class="fl-ing" itemprop="ingredients">\s#', null, $ingredients);
echo $ingredients;
理论上,应该删除 span id=lblIngAmount
部分的所有内容。我哪里错了? str_replace 前后的文本保持不变。怎么会?
感谢所有帮助!如果您需要更多详细信息,我很乐意提供!
最佳答案
关于php - Str_Replace 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14016415/