20223427范欣怡实验四报告

咦yi 2023-06-08 14:57:19

学号 2022-2023-2 Python程序设计》实验四报告
课程:《Python程序设计》
班级:2234
姓名:范欣怡
学号:20223427
实验教师:王志强
实验日期:2022515
必修/选修: 公选课

 1.实验内容
此处填写实验的具体内容;
设计爬虫程序,获取网页上的天气信息,并通过邮件的方式发送给自己。
 2. 实验过程及结果
1)实验过程

A.导入与爬虫有关的库

B.获取所爬取网页的url,并进行UA伪装

C.建立获取天气情况的正则表达式

D.建立字典,处理数据

E.将字典中的数据取出,进行处理

F.编写邮件发送代码

G.调试运行

2)实验源代码

import requests
import re
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

from Tools.scripts.which import msg

url =
'http://www.weather.com.cn/weather/101010900.shtml'

header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)'
                       'Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.1.1171 SLBChan/32'
}
response = requests.get(
url=url,headers=header)
response.encoding=
'utf-8'
weather_text = response.text
tem =
'<li class=".*?">.*?<h1>(.*?)</h1>'
tem2 = '</big>.*?<p title="(.*?)" class="wea"'
tem3 = '<p class="tem">.*?<i>(.*?)</i>'
tem4 = '<p class="tem">.*?<span>(.*?)</span>'
date_list = re.findall(tem,weather_text, re.S)
condition_list = re.findall(tem2
, weather_text, re.S)
min_tempture_list = re.findall(tem3
, weather_text, re.S)
max_tempture_list = re.findall(tem4
, weather_text, re.S)
weather_condition = {}
weather_tempture_min = {}
weather_tempture_max = {}

if len(max_tempture_list) == 6:
    weather_tempture_max[date_list[
0]] = ''
   
for each in range(0, 3):
        weather_condition[date_list[each]] = condition_list[each]
        weather_tempture_min[date_list[each]] = min_tempture_list[each]
   
for i in range(1, 3):
        weather_tempture_max[date_list[i]] = max_tempture_list[i]

else:
   
for each in range(0, 3):
        weather_condition[date_list[each]] = condition_list[each]
        weather_tempture_min[date_list[each]] = min_tempture_list[each]
        weather_tempture_max[date_list[each]] = max_tempture_list[each]

text1 = []

for n in range(0, 3):
    text1.append(
'日期:' + date_list[n] + '\n天气状况:' + weather_condition[date_list[n]] + '\n最低温--最高温:' +
                 weather_tempture_min[date_list[n]] +
'-' + weather_tempture_max[date_list[n]])
text2 = text1[
0] + '\n' + text1[1] + '\n ' + text1[2]
print(text2)
fromfxyyy =
'2463157225@qq.com'
password = 'pcjhiwlokpcdeaff'
tofxyyy = '2463157225@qq.com'
def mail():
    mail_text=text1[
0]
    ret =
True
    try
:
        msg = MIMEText(mail_text)
        msg[
'From'] = formataddr(["From@一只小游鱼", fromfxyyy])
        msg[
'To'] = formataddr(["一只小游鱼", tofxyyy])
        msg[
'Subject'] = "今日天气"

       
server = smtplib.SMTP_SSL("smtp.qq.com")
        server.login(fromfxyyy
, password)
        server.sendmail(fromfxyyy
, [tofxyyy], msg.as_string())
        server.quit()
   
except Exception:
        ret =
False
    return
ret
ret = mail()

if ret:
   
print("邮件发送成功")
else:
   
print("邮件发送失败")

3)实验截图


3.实验过程中遇到的问题和解决过程
问题1:一开始没有下载安装爬虫程序所需要的库和组件
问题1解决方案:在设置中查找安装
问题2:内容无法发送,只能在邮件中看到主题,内容显示为空


问题2解决方案:将原代码改写成了

mail_text=text1[0]

ret = True

try:

msg = MIMEText(mail_text)”
问题3:不能很好地运用正则表达式
问题3解决方案:查阅了书籍资料,进行相关内容的学习

4.实验感想

这一次的实验我做了很久,从找资料,看博客到真正地开始操作,编写代码的过程中遇到了很多的困难,这是因为我的水平实在有限。之前一直是用idle来编写代码的,这是我第一次在pycharm的环境里调试程序,很方便,简化了不少过程。一开始我只是按照学到的方法思路进行编写,后来我发现好像并不能达到我想要的效果,于是我又进行了新的尝试。实验成功的那一刻,我的心中充满了喜悦,越发的觉得Python是一门很神奇的语言,可以创造出许许多多的可能性。

5.课程总结

虽然只与Python接触了短短几个月的时间,但是我已经切实感受到了它的魅力。于我而言,编写程序的过程是艰难的,但是也是充满乐趣的。能够看到自己编写的代码在屏幕上跳跃,运行着,心中的快乐也是不言自明的。为期十六周的Python课程已经结束了,但是我们学习语言的脚步当然不应该就此停下。希望能在之后的闲暇时间里继续学习,不断提高我的Python水平,希望未来有一天能够不借助外力写出我自己的代码。
6.参考资料
1.CSDN博客 艾派森 《使用python自动发送邮件》

2.CSDN博客 苦苦的码人 Python爬取天气数据,并每天定时发送到QQ邮箱

3.CSDN博客 山与海 通过爬虫定期发送当地天气情况给自己的邮箱

 

 

...全文
237 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

144

社区成员

发帖
与我相关
我的任务
社区描述
开展Python教学和技术交流
python 高校 北京·丰台区
社区管理员
  • blackwall0321
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧