有时候大家想看看百度收录了我多少页面了,电脑又不在身边,手机操作又不方便,于是就写了这么个python脚本。
功能:
如果有新收录,发送邮件到指定邮箱
每个一小时监控一次。
发送新增页面,和总页面,以及其数量
import smtplib import time from email.mime.text import MIMEText import parsel import requests def get_info(): url = ' #这里改成你的网站即可 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36', } html = requests.get(url=url, headers=headers).text html = parsel.Selector(html) titles = html.xpath("//a[@class='col-blue02']/text()").extract() urls = html.xpath("//a[@class='col-blue02']/@href").extract() nums = len(titles) return { 'titles': titles, 'urls': urls, 'nums': nums } def send_mail(infomation, old_nums, old_titles, old_urls): if infomation['nums'] > old_nums: newnums = infomation['nums'] titles = infomation['titles'] urls = infomation['urls'] new_contents = '' for i in range(len(titles)): if titles[i] not in old_titles: new_contents += f'{titles[i]}\n{urls[i]}\n' mail_content = f'老大,截至{time.strftime("%Y年%m月%d日%H:%M:%S", time.localtime())}\n百度新收录:{newnums - old_nums}条\n总计收录:{newnums}条\n新收录信息如下:' mail_content += new_contents # 邮件内容 mail_content += '目前收录如下:\n' # By:www.lanol.cn # autor:Lan for i in range(len(titles)): mail_content += f'{titles[i]}\n{urls[i]}\n' try: content = MIMEText(mail_content, 'plain', 'utf-8') reveivers = "******@qq.com" # 收信邮箱 content['To'] = reveivers # 设置邮件的接收者,多个接收者之间用逗号隔开 content['From'] = str("admin@lanol.cn") # 邮件的发送者,最好写成str("这里填发送者"),不然可能会出现乱码 content['Subject'] = "老大,你的网站又被百度收录啦!!!" # 邮件的主题 smtp_server = smtplib.SMTP_SSL("{smtp服务器地址}", {服务器端口}) smtp_server.login("{发信邮箱账号}", "{发信邮箱密码}") smtp_server.sendmail("{发信邮箱}", ["{收信邮箱,多个用,隔开,这是个列表}"], content.as_string()) smtp_server.quit() except Exception as e: print(str(e)) except smtplib.SMTPException: print("Error: 无法发送邮件") return { 'newnums': newnums, 'titles': titles, 'urls': urls } if __name__ == '__main__': nums = 0 titles = [] urls = [] while True: new_infomation = send_mail(get_info(), nums, titles, urls) nums = new_infomation['newnums'] titles = new_infomation['titles'] urls = new_infomation['urls'] print(f'{time.strftime("%Y年%m月%d日%H:%M:%S", time.localtime())}发送成功') time.sleep(3600)
整套下载地址:
[url href=’https://ww.lanzous.com/icxlg1i’]蓝奏云下载[/url]
博客原文:让python自动给你报告百度新收录了多少页面,并发送到你的邮箱https://www.lanol.cn/post/238.html
评论前必须登录!
注册