最近媳妇让我盯着她关心的一个网页公告更新了没有,我就利用爬虫简单做了个监听标题发邮件的问题
implementation 'org.jsoup:jsoup:1.13.1' implementation group: 'com.sun.mail', name: 'javax.mail', version: '1.6.2'
package com.rex.tjavalibrary.jsoup
import org.jsoup.Jsoup
import java.util.*
import javax.mail.Message
import javax.mail.Session
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeMessage
// 个人笔记
object TestEmail {
// #如何获取授权码 https://baijiahao.baidu.com/s?id=1552315463915496&wfr=spider&for=pc
// 授权码
const val key = "你的授权码"
var lastTitle = ""
@JvmStatic
fun main(args: Array<String>) {
// 以qq为例子去邮箱设置查看
val properties = Properties()
properties["mail.transport.protocol"] = "smtp"
properties["mail.smtp.host"] = "smtp.qq.com"
properties["mail.smtp.port"] = 465
properties["mail.smtp.auth"] = "true"
properties["mail.smtp.ssl.enable"] = "true"
properties["mail.debug"] = "true"
while (true) {
try {
getNews(properties)
Thread.sleep(10 * 1000)
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
}
val targetUrl = "http://www.10.gov.cn/list-638-1.html"
private fun getNews(properties: Properties): Boolean {
try {
val url = targetUrl
// 直接连接
val connect = Jsoup.connect(url)
val document = connect.get()
document?.run {
// 此处需要根据实际网页进行分析 网页打开开发者模式 如下图 我直接搜索标题汉字 发现标签名是 txtlisty和a,属性名字分别是title,href
val item = document
.select(".txtlisty1")
.select("a")
item?.run {
val title = attr("title")
val href = attr("href")
return if (lastTitle != title) {
println("发现了新内容")
println("标题 = [$title]")
println("详细链接 = [$href]")
sendEm(properties, url, title, href)
lastTitle = title
true
} else {
println("${Date()} 网站没有任何更新仍然是旧内容:$title ")
false
}
}
}
return false
} catch (e: Exception) {
e.printStackTrace()
}
return false
}
private fun sendEm(
properties: Properties, url: String, title: String,
href: String
) {
// 得到回话对象
val session = Session.getInstance(properties)
// 获取邮件对象
val message: Message = MimeMessage(session)
// 设置发件人邮箱地址 需要至少两个qq号
val from = "你的发送邮件"
val to1 = "你的接收邮箱1"
val to2 = "你的接收邮箱2"
message.setFrom(InternetAddress(from))
// 设置收件人地址
message.setRecipients(
MimeMessage.RecipientType.TO, arrayOf(
InternetAddress(to1), InternetAddress(to2)
)
)
// 设置邮件标题
message.subject = "提示"
// 设置邮件内容
var context = "检测到监听网站已经更新内容了,快去查看吧:${url} $lastTitle \n $href"
if (lastTitle.isEmpty()) {
context =
"首次测试 开启监听 $targetUrl 更新内容后你将会收到 提醒${Date()} " +
"\n 当前最后一条内容是:$title"
}
message.setText(context)
// 得到邮差对象
val transport = session.transport
// 连接自己的邮箱账户
// 密码为刚才得到的授权码
transport.connect(from, key)
// 发送邮件
transport.sendMessage(message, message.allRecipients)
}
}