dongmeiwei0226
2017-10-10 16:18
浏览 54
已采纳

PHP file_get_contents booking.com网站

I try to get a booking.com page from a hotel to fetch the prices afterwards with regex. The problem is the following:

I call file_get_contents with parameter like checkin and checkout (file_get_contents("/hotel/at/myhotel.html?checkin=2017-10-12&checkout=2017-10-13")) dates so that the prices are shown to the visitor. If I watch the source code in the browser I see the entry:

b_this_url : '/hotel/at/myhotel.html?label=gen173nr-1FCAsoDkIcbmV1ZS1wb3N0LWhvbHpnYXUtaW0tbGVjaHRhbEgHYgVub3JlZmgOiAEBmAEHuAEHyAEM2AEB6AEB-AEDkgIBeagCAw;sid=58ccf750fc4acb908e20f0f28544c903;checkin=2017-10-12;checkout=2017-10-13;dist=0;sb_price_type=total;type=total&',

If I echo the string from file_get_contents the string looks like:

b_this_url : '/hotel/at/myhotel.html',

So all parameters that I passed to the url with file_get_contents are gone and therefore I couldn't find any prices with my regex on the page ...

Does anyone have a solution for this problem?

图片转代码服务由CSDN问答提供 功能建议

我尝试从酒店获取booking.com页面,然后使用正则表达式获取价格。 问题如下:

我使用checkin和checkout等参数调用file_get_contents( file_get_contents(“/ hotel / at / myhotel.html?checkin = 2017-10-12& checkout = 2017-10-13“))日期,以便向访客显示价格。 如果我在浏览器中查看源代码,我会看到条目:

  b_this_url:'/ hotel / at / myhotel.html?label = gen173nr-1FCAsoDkIcbmV1ZS1wb3N0LWhvbHpnYXUtaW0tbGVjaHRhbEgHYgVub3JlZmgOiAEBmAEHuAEHyAEM2AEB6AEB-AEDkgIBeagCAw; sid =  58ccf750fc4acb908e20f0f28544c903;签=二○一七年十月一十二日;结帐= 2017年10月13日; DIST = 0; sb_price_type =总;类型=总&安培;”,
   
 
 

如果 我回显来自file_get_contents的字符串,字符串如下:

  b_this_url:'/ hotel / at / myhotel.html',
   
  
 

所以我用file_get_contents传递给url的所有参数都消失了,因此我在页面上找不到我的正则表达式的任何价格......

有没有人有 这个问题的解决方案?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dst3605528 2017-10-10 17:22
    最佳回答

    The webpage is not completely generated server-side, but it relies heavily on JavaScript after the HTML part loads. If you are looking for rendering the page as it looks in browser, I think you should use php curl instead of file_get_contents() for this kind of web scrapping thing. I generated an automatic code for you from Postman (a google chrome extension / standalone desktop app) for your given url. The response contains the full url with params. See the image and I posted the code for you also.

    enter image description here

    <?php
    
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://www.booking.com/hotel/at/hilton-innsbruck.de.html?checkin=2017-10-10%3Bcheckout%3D2017-10-11",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_HTTPHEADER => array(
        "cache-control: no-cache",
        "postman-token: 581a75a7-6600-6ed6-75fd-5fb09c25d927"
      ),
    ));
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题