【问题标题】:How can I scrape the values from the source code of a particular url with PHP?如何使用 PHP 从特定 url 的源代码中抓取值?
【发布时间】:2020-08-24 13:02:42
【问题描述】:
在我想要抓取的网址的源代码中,我可以看到:
<div class="price">
22.96€
</div>
这是我创建的 php 文件,但它没有返回任何值:
<?php
$data_scraped = file_get_contents('https://www.example.com/es/busquedas/?type%5B%5D=steam&query=motogp+20');
$the_start = explode('<div class="price">',$data_scraped);
$the_end = explode('</div>',$the_start[1]);
echo $the_end[0];
?>
任何帮助将不胜感激
【问题讨论】:
标签:
php
html
web-scraping
【解决方案1】:
您的问题类似于this 一个使用simple_html_dom 在 PHP 中进行网络抓取的问题。
使用 simple_html_dom 重构您的代码:
<?php
require 'simple_html_dom.php';
$html = file_get_html('https://www.example.com/es/busquedas/?type%5B%5D=steam&query=motogp+20');
$price = $html->find('div[class=price]');
echo $price->plaintext."<br>";
?>