import requests import openpyxl import csv from bs4 import BeautifulSoup import random from urllib.request import quote import time import smtplib import schedule from email.mime.text import MIMEText from email.header import Header account = '[email protected]' password = 'hwg' receiver = ['[email protected] ' , ' [email protected] ' ] DEF movie_send (): # create excel storage crawling douban top250 movie information wb = openpyxl.Workbook () Sheet = wb.active sheet.title = ' movie_info ' Sheet [ ' A1 ' ] = ' ID ' Sheet [ ' Bl ' ] = ' movie name ' Sheet [ ' a C1 ' ] = ' movie connection ' sheet['D1'] ='文字简介' a=0 b=0while a<=225: headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' } url='https://movie.douban.com/top250?start={}&filter='.format(a) a=a+25 res=requests.get(url,headers=headers) bs=BeautifulSoup(res.text,'html.parser') items=bs.find_all(class_='item') for item in items: movie_name=item.find(class_='title').text movie_url =item.find('a')['href'] movie_inq =item.find(class_='inq').text movie_photo=item.find('img')[' The src ' ] B = B +. 1 movie_list.append (MOVIE_NAME) sheet.append ([B, MOVIE_NAME, movie_url, movie_inq]) # save the movie information into Excel wb.save ( ' D: python_reptile \\ \\ \\ pythontest douban_movie_list.xlsx ' ) Print ( ' OK ' ) # Excel read WB = openpyxl.load_workbook ( ' D: python_reptile \\ \\ \\ pythontest douban_movie_list.xlsx ' ) # call openpyxl.load_workbook () function, open the "* **. xlsx "file. WB = Sheet [ ' movie_info '# Get "Marvel.xlsx" workbook called "movie_info" worksheet # sheetname = wb.sheetnames # sheetnames is used to obtain the names of all worksheets workbook, if you do not know the workbook in the end there are several worksheets, you can put the name of the worksheet are printed. = movie_text '' # randomly selected three films, and print for I in Range (l, 4 ): NUM = the random.randint (1,250 ) B2 = Sheet [ ' B ' + STR (NUM)] # The "movie_info" Job table assigned to cell B2 B2_cell B2_value = B2.value # re-use property .value cell # this is the code of the download link climbing film movie = B2_value gbkmovie = movie.encode ( ' GBK') urlsearch = 'http://s.ygdy8.com/plus/s0.php?typeid=1&keyword='+quote(gbkmovie) res = requests.get(urlsearch) res.encoding='gbk' soup_movie = BeautifulSoup(res.text,'html.parser') urlpart=soup_movie.find(class_="co_content8").find_all('table') if urlpart: urlpart=urlpart[0].find('a')['href'] urlmovie='https://www.ygdy8.com/'+urlpart res1=requests.get(urlmovie) res1.encoding='gbk' soup_movie1=BeautifulSoup(res1.text,'html.parser') urldownload=soup_movie1.find('div',id="Zoom").find('span').find('table') .find ( ' A ' ) [ ' the href ' ] movie_all = '' ' .% d movie:% s Movie links:% s ' '' % (I, Movie, urldownload) movie_text = movie_text + movie_all the else : movie_miss = ' no ' + + movie ' link ' movie_all = '' ' of film% d:% s movie links:% s ' '' % (I, movie,movie_miss) movie_textmovie_text + = movie_all return movie_text DEF SEND_EMAIL (movie_text): # Package SEND_EMAIL () function, used to send messages # 1, the connection server the mailhost = ' smtp.qq.com ' # mailing address of the server assigned to the variable the mailhost QQMail smtplib.SMTP_SSL = ( the mailhost) # instantiated object smtplib SMTP module qqmail.connect (the mailhost, 465) # connection to the server, passing two parameters, one parameter: the server address, parameter 2: port number. # 2, Log qqmail.login (the Account, password) # call .login () method Login Email # 3, text acquisition module introduced Headr and MIMEText Content = movie_text Print(Content) # printing text, for viewing Message = MimeText (Content, ' Plain ' , ' UTF-. 8 ' ) # instantiate a message object MIEIText, three arguments: the message body text, encoded. # In the right hand side instantiate a Header object that passes in two parameters: the message subject, coding. Then variable assignment to the left operand Subject = ' random recommended three movies ' # mail subject the Message [ ' the From ' ] = Header (the Account) # sender information the Message [ ' the To ' ] = Header ( " , " .join (Receiver)) # recipient information Message [ ' the Subject ' ] = Header (Subject, ' UTF-. 8 ' ) # mail subject # 4, send a message, calling the sendmail () method, passing three parameters: 1, senders, 2, recipient, 3, the text string the try : qqmail.sendmail (the Account, Receiver, message.as_string ()) Print ( ' e-mail sent successfully ' ) the except : Print ( ' e-mail sending failure ' ) # 5, exit the mailbox qqmail.quit () DEF the job (): Print ( ' the beginning of a mission ' ) movie_textMovie_send = () # call weather_spider () function and the returned value is assigned to the variable SEND_EMAIL (movie_text) # call send_email () arguments passed Print ( ' task completion ' ) schedule.every (). Friday.at ( " 17 : 00 " ) .do (the Job) the while True: schedule.run_pending () the time.sleep ( 1)
#operation result