fix scrape failure when using quotes on brave
This commit is contained in:
+39
-40
@@ -212,60 +212,43 @@ class brave{
|
|||||||
|
|
||||||
private function get_js(){
|
private function get_js(){
|
||||||
|
|
||||||
$script_disc =
|
$script_bodies =
|
||||||
$this->fuckhtml
|
$this->fuckhtml
|
||||||
->getElementsByTagName(
|
->getElementsByTagName(
|
||||||
"script"
|
"script"
|
||||||
);
|
);
|
||||||
|
|
||||||
$data = null;
|
$datas = [];
|
||||||
foreach($script_disc as &$discs){
|
foreach($script_bodies as &$script){
|
||||||
|
|
||||||
if(
|
$jsons = explode("{type:\"data\"", $script["innerHTML"]);
|
||||||
preg_match(
|
array_shift($jsons);
|
||||||
'/kit\.start\(/',
|
|
||||||
$discs["innerHTML"]
|
|
||||||
)
|
|
||||||
){
|
|
||||||
|
|
||||||
$data =
|
foreach($jsons as $json){
|
||||||
explode(
|
|
||||||
"function(",
|
|
||||||
$discs["innerHTML"]
|
|
||||||
);
|
|
||||||
|
|
||||||
$data = $data[count($data) - 1];
|
|
||||||
|
|
||||||
$data =
|
|
||||||
explode(
|
|
||||||
"return ",
|
|
||||||
$data,
|
|
||||||
2
|
|
||||||
)[1];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if($data === null){
|
|
||||||
|
|
||||||
throw new Exception("Could not grep JavaScript object");
|
|
||||||
}
|
|
||||||
|
|
||||||
$data =
|
$data =
|
||||||
$this->fuckhtml
|
$this->fuckhtml
|
||||||
->parseJsObject(
|
->parseJsObject(
|
||||||
$this->fuckhtml
|
$this->fuckhtml
|
||||||
->extract_json(
|
->extract_json(
|
||||||
$data
|
"{type:\"data\"" .
|
||||||
|
$json
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
if($data === null){
|
if($data !== []){
|
||||||
|
|
||||||
throw new Exception("Failed to decode JavaScript object");
|
$datas[] = $data;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $data;
|
if(count($datas) === 0){
|
||||||
|
|
||||||
|
throw new Exception("Could not grep any JavaScript object");
|
||||||
|
}
|
||||||
|
|
||||||
|
return $datas;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function web($get){
|
public function web($get){
|
||||||
@@ -440,8 +423,11 @@ class brave{
|
|||||||
|
|
||||||
// do some magic
|
// do some magic
|
||||||
$this->fuckhtml->load($html);
|
$this->fuckhtml->load($html);
|
||||||
$data = $this->get_js();
|
$datas = $this->get_js();
|
||||||
|
|
||||||
|
$has_found_body = false;
|
||||||
|
|
||||||
|
foreach($datas as $data){
|
||||||
if(
|
if(
|
||||||
isset($data[2]["data"]["title"]) &&
|
isset($data[2]["data"]["title"]) &&
|
||||||
stripos($data[2]["data"]["title"], "PoW Captcha") !== false
|
stripos($data[2]["data"]["title"], "PoW Captcha") !== false
|
||||||
@@ -452,9 +438,10 @@ class brave{
|
|||||||
|
|
||||||
if(!isset($data["data"]["body"]["response"])){
|
if(!isset($data["data"]["body"]["response"])){
|
||||||
|
|
||||||
throw new Exception("Brave did not return a result object");
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$has_found_body = true;
|
||||||
$data = $data["data"]["body"]["response"];
|
$data = $data["data"]["body"]["response"];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -1123,6 +1110,12 @@ class brave{
|
|||||||
|
|
||||||
// append discussions at position 2
|
// append discussions at position 2
|
||||||
array_splice($out["web"], 1, 0, $disc_out);
|
array_splice($out["web"], 1, 0, $disc_out);
|
||||||
|
}
|
||||||
|
|
||||||
|
if($has_found_body === false){
|
||||||
|
|
||||||
|
throw new Exception("Brave did not return a search result object");
|
||||||
|
}
|
||||||
|
|
||||||
return $out;
|
return $out;
|
||||||
}
|
}
|
||||||
@@ -1223,8 +1216,9 @@ class brave{
|
|||||||
);
|
);
|
||||||
|
|
||||||
$this->fuckhtml->load($html);
|
$this->fuckhtml->load($html);
|
||||||
$json = $this->get_js();
|
$jsons = $this->get_js();
|
||||||
|
|
||||||
|
foreach($jsons as $json){
|
||||||
foreach(
|
foreach(
|
||||||
$json["data"]["response"]["news"]["results"]
|
$json["data"]["response"]["news"]["results"]
|
||||||
as $news
|
as $news
|
||||||
@@ -1256,6 +1250,7 @@ class brave{
|
|||||||
"url" => $news["url"]
|
"url" => $news["url"]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $out;
|
return $out;
|
||||||
}
|
}
|
||||||
@@ -1309,8 +1304,9 @@ class brave{
|
|||||||
|
|
||||||
$this->detect_captcha($html);
|
$this->detect_captcha($html);
|
||||||
|
|
||||||
$json = $this->get_js();
|
$jsons = $this->get_js();
|
||||||
|
|
||||||
|
foreach($jsons as $json){
|
||||||
foreach(
|
foreach(
|
||||||
$json
|
$json
|
||||||
["data"]
|
["data"]
|
||||||
@@ -1337,6 +1333,7 @@ class brave{
|
|||||||
"url" => $result["url"]
|
"url" => $result["url"]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $out;
|
return $out;
|
||||||
}
|
}
|
||||||
@@ -1442,8 +1439,9 @@ class brave{
|
|||||||
fclose($handle);*/
|
fclose($handle);*/
|
||||||
|
|
||||||
$this->fuckhtml->load($html);
|
$this->fuckhtml->load($html);
|
||||||
$json = $this->get_js();
|
$jsons = $this->get_js();
|
||||||
|
|
||||||
|
foreach($jsons as $json){
|
||||||
foreach(
|
foreach(
|
||||||
$json
|
$json
|
||||||
["data"]
|
["data"]
|
||||||
@@ -1494,6 +1492,7 @@ class brave{
|
|||||||
"url" => $result["url"]
|
"url" => $result["url"]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $out;
|
return $out;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user