我是使用PHP而不是PHP本身的Web抓取新手.我的问题不是正则表达式相关,但似乎直接与booking.com网站有关.我想在特定城市刮取酒店的价格.为此,我在预订页面中复制了浏览器中的URL,并将其粘贴到我的代码中.
这是页面.
这是我的代码:
<?php
function getHTML($url,$timeout)
{
$ch = curl_init($url); // initialize curl with given url
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set useragent
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
return @curl_exec($ch);
}
$html=getHTML("http://www.booking.com/searchresults.en.html?dcid=1;checkin_monthday=25;checkin_year_month=2014-7;checkout_monthday=26;checkout_year_month=2014-7;city=-1461464;class_interval=1;csflt=%7B%7D;interval_of_time=undef;no_rooms=1;or_radius=0;property_room_info=1;review_score_group=empty;score_min=0;src=city;ssb=empty;;nflt=ht_id%3D204%3Bclass%3D3%3B;unchecked_filter=class",10);
echo $html;
?>
Run Code Online (Sandbox Code Playgroud)
我确实打印了一个预订页面,但它没有考虑到URL中的参数,因为在页面上我得到它要求预订日期和城市...
我尝试在几个浏览器中粘贴此URL并隐藏窗口(以查看URL是否链接到Cookie或其他内容),并且它运行正常.也许我在cURL请求中错过了一个参数......