0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

试试虚拟主机的计划任务

Posted at

之前发现虚拟主机有计划任务的功能,为什么不用起来呢.
比如说,我发现一个网站每天会更新一些内容, 虽然很多网站他们也会每天有邮件推送.
但是内容不可控,有很多额外的东西, 那就不是我想要的了.

所以,我有个需求就是每天去访问一个地址,然后挑出我想要的,然后发邮件给我.
以为虚拟主机你无法自由安装依赖,所以在需要的一些库的时候直接就包含在你自己的代码里就行了.
我选了2个库

  1. 编辑 HTML 的: http://sourceforge.net/projects/simplehtmldom/
  2. 还有就是发邮件的 PHPMailer

剩下的其实需要做的很少了,就是获取一个网页,取到你想要的内容, 然后发邮件.
比如, 我发现一个日英翻译的网站,每天会列出一些常见使用错误的英语用法, 我就截取到它,然后邮件给我

// Create DOM from URL or file
$html = file_get_html('http://dictionary.goo.ne.jp/mistake_english');
$content = '';
// Find all images 
foreach($html->find('div.text_honbun_box') as $element) {
	   $content = $element->plaintext . '<br>';
}
    

$mail = new PHPMailer;
$mail->setFrom('显示用的发件人地址', '显示发件人名字');
$mail->Subject = '主题';
$mail->addAddress('收件人地址', '收件人名字');
$mail->Body    = $content;

if(!$mail->Send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}

然后就是去虚拟主机管理面板添加计划任务了. 这个也就是 linux 系统的 cronjob 了.
比如我把上面的代码文件保存为 crawl.php, 放到了我的 root 目录,就这样写它:

24	13	*	*	*	/usr/local/bin/php /home/你的用户名/crawl.php >/dev/null 2>&1

done!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?