如何使用DOMDocument PHP从网站获取类值

时间:2018-10-14 07:53:20

标签: php domdocument domxpath

我正在尝试从网站url获取特定的类。我已经尝试过使用下面的代码,但由于得到503响应,因此无法获得loadHTML

// <span class="_1n0q8zmp">AUD - $</span>

$url = 'https://www.airbnb.com/rooms/19844318';
function file_get_contents_curl($url) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36');
  $html = curl_exec($ch);
  curl_close($ch);
  return $html;
}
$html = file_get_contents_curl($url);
$dom = new DOMDocument();
libxml_use_internal_errors(1);
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
$script = $dom->getElementsByTagName('span');
$script = $xpath->query("//*[contains(@class, '_1n0q8zmp')]");

echo $script;
// result should be: AUD - $

0 个答案:

没有答案