43

Googleニュースの各記事の抜粋の下部にソースがあることに注目してください。

ガーディアン-ABCニュース-ロイター-ブルームバーグ

私はそれを真似ようとしています。

たとえば、URLhttp://www.washingtontimes.com/news/2010/dec/3/debt-panel-fails-test-vote/を送信すると、返送したいThe Washington Times

これはphpでどのように可能ですか?

4

9 に答える 9

66

私の答えは、ページのタイトルを使用する@AI Wの答えを拡張しています。以下は、彼が言ったことを達成するためのコードです。

<?php

function get_title($url){
  $str = file_get_contents($url);
  if(strlen($str)>0){
    $str = trim(preg_replace('/\s+/', ' ', $str)); // supports line breaks inside <title>
    preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case
    return $title[1];
  }
}
//Example:
echo get_title("http://www.washingtontimes.com/");

?>

出力

ワシントン タイムズ - 政治、ニュース速報、米国および世界のニュース

ご覧のとおり、これは Google が使用しているものとまったく同じではないため、Google は URL のホスト名を取得し、それを独自のリストと照合していると思われます。

http://www.washingtontimes.com/ => ワシントン・タイムズ

于 2010-12-03T19:20:00.677 に答える
36
$doc = new DOMDocument();
@$doc->loadHTMLFile('http://www.washingtontimes.com/news/2010/dec/3/debt-panel-fails-test-vote/');
$xpath = new DOMXPath($doc);
echo $xpath->query('//title')->item(0)->nodeValue."\n";

出力:

債務委員会はテスト投票で不十分です-ワシントンタイムズ

明らかに、基本的なエラー処理も実装する必要があります。

于 2010-12-03T19:15:17.040 に答える
6

ドメインのホームページから get_meta_tags() を使用すると、NYT の場合、切り捨てが必要になる可能性がありますが、役立つ可能性があるものが返されます。

$b = "http://www.washingtontimes.com/news/2010/dec/3/debt-panel-fails-test-vote/" ;

$url = parse_url( $b ) ;

$tags = get_meta_tags( $url['scheme'].'://'.$url['host'] );
var_dump( $tags );

「ワシントン タイムズは、わが国の将来に影響を与える問題に関するニュース速報と解説を配信します。」という説明が含まれています。

于 2010-12-03T20:30:21.823 に答える
5

titleURLのコンテンツをフェッチして、要素のコンテンツの正規表現検索を実行できます。

<?php
$urlContents = file_get_contents("http://example.com/");
preg_match("/<title>(.*)<\/title>/i", $urlContents, $matches);

print($matches[1] . "\n"); // "Example Web Page"
?>

または、正規表現を使用したくない場合(ドキュメントの上部に非常に近いものと一致させるため)、DOMDocumentオブジェクトを使用できます。

<?php
$urlContents = file_get_contents("http://example.com/");

$dom = new DOMDocument();
@$dom->loadHTML($urlContents);

$title = $dom->getElementsByTagName('title');

print($title->item(0)->nodeValue . "\n"); // "Example Web Page"
?>

どの方法が一番好きかはあなたに任せます。

于 2010-12-03T19:03:23.267 に答える
4

cURLに関するPHPマニュアル

<?php

$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

Perl正規表現マッチングに関するPHPマニュアル

<?php
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>

そして、これら2つを組み合わせると:

<?php 
// create curl resource 
$ch = curl_init(); 

// set url 
curl_setopt($ch, CURLOPT_URL, "example.com"); 

//return the transfer as a string 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

// $output contains the output string 
$output = curl_exec($ch); 

$pattern = '/[<]title[>]([^<]*)[<][\/]titl/i';

preg_match($pattern, $output, $matches);

print_r($matches);

// close curl resource to free up system resources 
curl_close($ch);      
?>

ここにPHPがないため、この例が機能することを約束することはできませんが、開始するのに役立つはずです。

于 2010-12-03T19:03:23.520 に答える
1

または、 Simple HtmlDomParserを使用することもできます。

<?php
require_once('simple_html_dom.php');

$html = file_get_html('http://www.washingtontimes.com/news/2010/dec/3/debt-panel-fails-test-vote/');

echo $html->find('title', 0)->innertext . "<br>\n";

echo $html->find('div[class=entry-content]', 0)->innertext;
于 2010-12-03T19:25:14.753 に答える
1

リンクからウェブサイトのタイトルを取得し、タイトルを utf-8 文字エンコーディングに変換します:</p>

https://gist.github.com/kisexu/b64bc6ab787f302ae838

function getTitle($url)
{
    // get html via url
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $html = curl_exec($ch);
    curl_close($ch);

    // get title
    preg_match('/(?<=<title>).+(?=<\/title>)/iU', $html, $match);
    $title = empty($match[0]) ? 'Untitled' : $match[0];
    $title = trim($title);

    // convert title to utf-8 character encoding
    if ($title != 'Untitled') {
        preg_match('/(?<=charset\=).+(?=\")/iU', $html, $match);
        if (!empty($match[0])) {
            $charset = str_replace('"', '', $match[0]);
            $charset = str_replace("'", '', $charset);
            $charset = strtolower( trim($charset) );
            if ($charset != 'utf-8') {
                $title = iconv($charset, 'utf-8', $title);
            }
        }
    }

    return $title;
}
于 2013-07-13T07:22:03.023 に答える