我需要帮助才能使用php script
从互联网上下载网页。但是现在我有从互联网下载网页的脚本。但是它正在下载与index.html名称一样的名称的网页。
我想在网址下载带有自己名字的网页。使用aboutus.html下载aboutus页面
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form method="post">
<input name="url" size="50" placeholder="URL" />
<input name="submit" type="submit" />
</form>
</body>
</html>
<?php
// maximum execution time in seconds
set_time_limit (24 * 60 * 60);
if (isset($_POST['submit'])) {
$url = parse_url($_POST['url']);
$folder = $url['host'];
if (array_key_exists('path', $url)) {
$file = explode('.', str_replace('/', '', $url['path']));
$file .= '.html';
} else {
$file = 'index.html';
}
if (!sizeOf(glob($folder))) {
mkdir($folder);
}
file_put_contents($folder . '/' . $file, fopen($_POST['url'], 'r'));
}
?>
答案 0 :(得分:2)
试试这个:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form method="post">
<input name="url" size="50" placeholder="URL" />
<input name="submit" type="submit" />
</form>
</body>
</html>
<?php
// maximum execution time in seconds
set_time_limit (24 * 60 * 60);
function get_title($url){
$str = file_get_contents($url);
if(strlen($str)>0){
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTML($str);
$title = $dom->getElementsByTagName( "title" );
$titleText = 'index';
if($title && $title->length){
$titleText = $title[0]->textContent;
}
libxml_use_internal_errors(false);
return $titleText;
}
}
if (isset($_POST['submit'])) {
$url = parse_url($_POST['url']);
$folder = $url['host'];
if (array_key_exists('path', $url)) {
$file = get_title($_POST['url']);
$file .= '.html';
} else {
$file = 'index.html';
}
if (!sizeOf(glob($folder))) {
mkdir($folder);
}
file_put_contents($folder . '/' . $file, fopen($_POST['url'], 'r'));
}
?>
答案 1 :(得分:1)
注意强>
根据
与answer provided by Adolfo Garza相反,使用正则表达式不是HTML的好主意,而是使用DOM Parser
<?php
function get_title( $url ){
$html = new simple_html_dom();
$html->load_file( $url );
$title = $html->find( 'title' );
return $title->plaintext;
}
if( isset( $_POST['submit'] ) ){
$url = parse_url( $_POST['url'] );
$folder = $url['host'];
if( array_key_exists( 'path', $url ) ){
$file = get_title( $_POST['url'] );
$file .= '.html';
}else{
$file = 'index.html';
}
if( !sizeOf( glob( $folder ) ) ){
mkdir( $folder );
}
file_put_contents($folder . '/' . $file, fopen($_POST['url'], 'r'));
}?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form method="post">
<input name="url" size="50" placeholder="URL" />
<input name="submit" type="submit" />
</form>
</body>
</html>