Laravel Guzzle getting destination and due time from a page

烈酒焚心 提交于 2019-12-13 08:27:27

问题


I want my website to scrape information from this page beta.tfgm.com/public-transport/tram/stops/oldham-central-tra‌​m and in particular I want to scrape destination and due time from that website and give me an output.

At this moment my controller looks like this:

function guzzle(){
$client = new \GuzzleHttp\Client();
$results = $client->get('https://beta.tfgm.com/public-transport/tram/stops/oldham-central-tram');
//$result->getBody();
//return view('hello')->with('results', $results);
//dd($result);
$results->getBody()->getContents();
dd($results);
return view('hello');
}

And the output is:

Response {#190 ▼
  -reasonPhrase: "OK"
  -statusCode: 200
  -headers: array:8 [▼
    "Content-Security-Policy" => array:1 [▶]
    "Content-Type" => array:1 [▶]
    "Date" => array:1 [▶]
    "ETag" => array:1 [▶]
    "X-Content-Security-Policy" => array:1 [▶]
    "X-WebKit-CSP" => array:1 [▶]
    "Content-Length" => array:1 [▶]
    "Connection" => array:1 [▶]
  ]
  -headerNames: array:8 [▼
    "content-security-policy" => "Content-Security-Policy"
    "content-type" => "Content-Type"
    "date" => "Date"
    "etag" => "ETag"
    "x-content-security-policy" => "X-Content-Security-Policy"
    "x-webkit-csp" => "X-WebKit-CSP"
    "content-length" => "Content-Length"
    "connection" => "Connection"
  ]
  -protocol: "1.1"
  -stream: Stream {#188 ▶}
}

However how can I access specific html class, scrape information from it and output it on my page?


回答1:


dd is not useful for you here, try this :

$client = new \GuzzleHttp\Client();
$results = $client->get('https://beta.tfgm.com/public-transport/tram/stops/oldham-central-tram');

print_r($results->getBody()->getContents()); // The HTML content


来源:https://stackoverflow.com/questions/44265891/laravel-guzzle-getting-destination-and-due-time-from-a-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!