如何从 https://www.fiverr.com/ 抓取数据
How to scrape data from https://www.fiverr.com/
抓取数据
基本上我实际上需要从以下子页面读取数据
https://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48
$url = 'https://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48';
$html = file_get_contents( $url);
但是它没有返回数据。
我也尝试过卷曲。
完全没有成功。
有什么想法吗?
您尝试获取的网站会尝试设置一些 cookie,然后告诉您使用新 cookie 向同一个 URL 发送另一个请求。 PHP 的 file_get_contents()
默认情况下不发送 cookie,但它遵循重定向,这意味着您进入 302 重定向循环。
为避免此循环,您需要手动设置 cookies :
$url = 'http://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48';
$opts = array('http' => array(
'header' => 'Cookie: locale=en%3B0%3Bfalse; suggested_locale=1;',
));
$ctx = stream_context_create($opts);
$data = file_get_contents($url, false, $ctx);
变量 $data
包含二进制数据,因为网站对内容进行了 gzip 压缩。所以你可能想要纯数据:
$data = gzdecode($data);
现在您有了 JSON 编码的数据,您可以使用 json_decode()
进行解析。
基本上我实际上需要从以下子页面读取数据 https://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48
$url = 'https://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48';
$html = file_get_contents( $url);
但是它没有返回数据。 我也尝试过卷曲。 完全没有成功。
有什么想法吗?
您尝试获取的网站会尝试设置一些 cookie,然后告诉您使用新 cookie 向同一个 URL 发送另一个请求。 PHP 的 file_get_contents()
默认情况下不发送 cookie,但它遵循重定向,这意味着您进入 302 重定向循环。
为避免此循环,您需要手动设置 cookies :
$url = 'http://www.fiverr.com/gigs/gigs_as_json?host=search&type=single_query&query_string=pet&search_filter=rating&category_id=3&sub_category_id=49&limit=48';
$opts = array('http' => array(
'header' => 'Cookie: locale=en%3B0%3Bfalse; suggested_locale=1;',
));
$ctx = stream_context_create($opts);
$data = file_get_contents($url, false, $ctx);
变量 $data
包含二进制数据,因为网站对内容进行了 gzip 压缩。所以你可能想要纯数据:
$data = gzdecode($data);
现在您有了 JSON 编码的数据,您可以使用 json_decode()
进行解析。