liminghu 发表于 2020-6-18 19:16:48

哪位大神帮忙看看怎么解决这个问题呀!跪求!!!

{:5_109:}

from bs4 import BeautifulSoup
import requests
import smtplib
from email.mime.text import MIMEText
import time
import bs4



class GrabNews():
    def __init__(self):
      self.NewsList = []
    def getNews(self):
      url = 'http://graduatedstudies.ju.edu.jo/ar/arabic/Home.aspx'
      r = requests.get(url)
      soup = bs4.BeautifulSoup(r.text, "html.parser")
      newsTitle = soup.find(text="الاعلانات")
      newsList = soup.find_all('a')
      for news in newsList:
            for string in news.stripped_strings:
                newsUrl = 'http://graduatedstudies.ju.edu.jo/' + news['href']
                self.NewsList.append({string:newsUrl})

def getNews():
    grabNews = GrabNews()
    grabNews.getNews()
    with open('news.html', 'w') as file:
      for news in grabNews.NewsList:
            for key in news.keys():# key:value. key是新闻标题,value是新闻链接
                file.write('<a href=%s>%s</a>' % (news, '*'+key))
                file.write('<hr />')

def sendNews():
    # 第三方 SMTP 服务
    mail_host = 'smtp.gmail.com'# SMTP服务器
    mail_user = 'liminghu91@gmail.com'# 用户名
    mail_pass = 'Lmh123454321'

    # 收发方地址
    sender = 'liminghu91@gmail.com'# 发件人邮箱
    receivers = ['630797301@qq.com']# 接收邮件

    # 从HTML文件中读取发送邮件的内容
    fp = open('news.html')
    message = MIMEText(fp.read(), 'html')# 内容, 格式, 编码
    fp.close()

    # 补全消息头部信息
    message['Subject'] = '约旦大学研究生学院公告'
    message['From'] = "{}".format(sender)
    message['To'] = ",".join(receivers)

    try:
      smtpObj = smtplib.SMTP(mail_host, 25)# SMTP协议默认端口是25
      smtpObj.login(mail_user, mail_pass)# 登录验证
      smtpObj.sendmail(sender, receivers, message.as_string())# 发送
      print('mail has been send successfully.')
    except smtplib.SMTPException as e:
      print(e)


if __name__ == '__main__':
    while(True):
      if('14:11'==time.strftime('%H:%M')):
            getNews()
            sendNews()
            time.sleep(61)# 等待一分钟以上
      else:
            time.sleep(30)# 每30秒查询一次系统时间

提示:

file.write('<a href=%s>%s</a>' % (news, '*'+key))
UnicodeEncodeError: 'gbk' codec can't encode character '\u0627' in position 64: illegal multibyte sequence

Process finished with exit code 1

鬼知道解决了之后还会有多少坑在前面{:5_100:}

qiuyouzhi 发表于 2020-6-18 19:41:00

打开方式改成这样:
with open('news.html', 'w', encoding = "utf-8") as file:

suchocolate 发表于 2020-6-18 19:42:15

getNews函数里:with open('news.html', 'w', encoding='utf-8') as file:

永恒的蓝色梦想 发表于 2020-6-18 19:43:57

from bs4 import BeautifulSoup
import requests
import smtplib
from email.mime.text import MIMEText
import time
import bs4



class GrabNews():
    def __init__(self):
      self.NewsList = []
    def getNews(self):
      url = 'http://graduatedstudies.ju.edu.jo/ar/arabic/Home.aspx'
      r = requests.get(url)
      soup = bs4.BeautifulSoup(r.text, "html.parser")
      newsTitle = soup.find(text="الاعلانات")
      newsList = soup.find_all('a')
      for news in newsList:
            for string in news.stripped_strings:
                newsUrl = 'http://graduatedstudies.ju.edu.jo/' + news['href']
                self.NewsList.append({string:newsUrl})

def getNews():
    grabNews = GrabNews()
    grabNews.getNews()
    with open('news.html', 'w', encoding='UTF-8') as file:
      for news in grabNews.NewsList:
            for key in news.keys():# key:value. key是新闻标题,value是新闻链接
                file.write('<a href=%s>%s</a>' % (news, '*'+key))
                file.write('<hr />')

def sendNews():
    # 第三方 SMTP 服务
    mail_host = 'smtp.gmail.com'# SMTP服务器
    mail_user = 'liminghu91@gmail.com'# 用户名
    mail_pass = 'Lmh123454321'

    # 收发方地址
    sender = 'liminghu91@gmail.com'# 发件人邮箱
    receivers = ['630797301@qq.com']# 接收邮件

    # 从HTML文件中读取发送邮件的内容
    fp = open('news.html')
    message = MIMEText(fp.read(), 'html')# 内容, 格式, 编码
    fp.close()

    # 补全消息头部信息
    message['Subject'] = '约旦大学研究生学院公告'
    message['From'] = "{}".format(sender)
    message['To'] = ",".join(receivers)

    try:
      smtpObj = smtplib.SMTP(mail_host, 25)# SMTP协议默认端口是25
      smtpObj.login(mail_user, mail_pass)# 登录验证
      smtpObj.sendmail(sender, receivers, message.as_string())# 发送
      print('mail has been send successfully.')
    except smtplib.SMTPException as e:
      print(e)


if __name__ == '__main__':
    while(True):
      if('14:11'==time.strftime('%H:%M')):
            getNews()
            sendNews()
            time.sleep(61)# 等待一分钟以上
      else:
            time.sleep(30)# 每30秒查询一次系统时间
页: [1]
查看完整版本: 哪位大神帮忙看看怎么解决这个问题呀!跪求!!!