-
Notifications
You must be signed in to change notification settings - Fork 0
/
seo-meta-data-send-daily.py
83 lines (70 loc) · 2.81 KB
/
seo-meta-data-send-daily.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Wrote by Jacky Yuan @2016
# To run this, you can install BeautifulSoup
# https://pypi.python.org/pypi/beautifulsoup4
import urllib.request, urllib.parse, urllib.error
from urllib.request import urlopen
from bs4 import BeautifulSoup
import ssl
import re
import numpy
import os.path
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
#fname = input('Enter File:')
fname = "GP1-website.txt"
f=open(fname,'r')
fout = open('GP1-autofusion-website-meta-data.txt','w')
#print(f)
for url in f:
url = url.rstrip()
html = urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, "html.parser")
fout.write("Client Website:"+(url)+"\n")
metas = soup.find_all('meta')
try:
fout.write("1st H1 Tag:"+str(soup.find_all('h1')[0].text.strip())+"\n")
fout.write("2nd H1 Tag:"+str(soup.find_all('h1')[1].text.strip())+"\n")
fout.write("3rd H1 Tag:"+str(soup.find_all('h1')[2].text.strip())+"\n")
except:
fout.write("H1 Tag:"+str("Attention:None H1 Exist!")+"\n")
fout.write("Meta Title:"+str(soup.title)+"\n")
metastring= str([ meta.attrs['content'] for meta in metas if 'name' in meta.attrs and meta.attrs['name'] == 'description' ])+"\n\n"
fout.write("Meta Description:"+metastring)
f.close()
#print (fout)
#print (soup.h1)
#print (soup.title)
#print ([ meta.attrs['content'] for meta in metas if 'name' in meta.attrs and meta.attrs['name'] == 'description' ])
#print (url)
fout.close()
email_user = '[email protected]' # Your Email Address
email_password = 'password!' #email_password
email_send = ['[email protected]'] # email address you want to send
subject = 'GP1 Daily Meta Data Result'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = ", ".join(email_send)
msg['Subject'] = subject
message = 'Hi there,\r\r\nSending this email from analytics team <H1 & Meta Data> Report for GP1 Client!\r\r\nOpen the txt file and Search(cmd+F) "Attention" to find out any missing H1 Tag.\r\r\nThank you! - Jacky Yuan\r\r\nSee the attached.'
msg.attach(MIMEText(message,'plain'))
filename='GP1-autofusion-website-meta-data.txt'
attachment =open(filename,'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+filename)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user,email_password)
server.sendmail(email_user,email_send,text)
server.quit()
print("Success: Email sent!")