Yahoo news IT記事を取得する{定番}
自分の記事に
Yahoo news IT記事を取得するという定番の記事が無かったので書いてみました。
yahoo newsのhtmlコードが変更されるとPHPのプログラムコードの修正が発生します。
yahoo_news.php
<?php
session_start();
class yahoo_{
function main(){
return new class{
var $xpath = null;
function gethtml($url){
$html = file_get_contents($url);
$dom = new DOMDocument();
$html = mb_convert_encoding($html, "HTML-ENTITIES", 'UTF-8');
@$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$this->xpath = new DOMXPath($dom);
return new class($this->xpath){
var $xpath_obj = null;
function __construct($xpath){
$this->xpath_obj = $xpath;
}
function yahoo_news_it(){
$res =[];
foreach($this->xpath_obj->query("//*[@id=\"uamods-topics\"]/div/div/div/ul/li") as $obj){
$res[] = $obj->textContent;
}
return $res;
}
};
}
};
}
}
$ret = [];
if($_SESSION["token"] === $_POST["token"]){
$yahoo_ = new yahoo_();
$ret["view"] = $yahoo_->main()->gethtml("https://news.yahoo.co.jp/categories/it")->yahoo_news_it();
}
print json_encode($ret);
<?php
session_start();
$token_byte = openssl_random_pseudo_bytes(16);
$_SESSION["token"] = $token = bin2hex($token_byte);
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="Description" content="Enter your description here"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<link rel="stylesheet" href="assets/css/style.css">
<title>yahoo_news</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-12">
<div id="view"></div>
<input type="hidden" name="" value="<?=$token?>" id="token">
<button class="btn btn-primary" id="btn" type="button">yahoo_newsを取得する</button>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js"></script>
<script>
document.getElementById("btn").addEventListener("click",function(){
$.ajax({
type: "post",
url: "./yahoo_news.php",
data:{token:document.getElementById("token").value},
dataType: "json",
success: function (response) {
if(response){
document.getElementById("view").innerHTML = ((res)=>{
var str = "";
for (const key in res) {
str+=res[key] + "<br>";
}
return str;
})(response.view);
}
}
});
});
</script>
</body>
</html>
Author And Source
この問題について(Yahoo news IT記事を取得する{定番}), 我々は、より多くの情報をここで見つけました https://qiita.com/taoka-toshiaki/items/a7a8758dad32410e5643著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .