20223410 李展鹏 《Python程序设计》实验四报告

223410木子展鹏 2024-05-29 22:22:24

课程:《Python程序设计》
班级: 2234
姓名: 李展鹏
学号:20223410
实验教师:王志强
实验日期:2024年5月15日
必修/选修: 公选课

1.实验内容
(1) 实验整体内容:

Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等。

(1)编写从社交网络爬取数据,实现可视化舆情监控或者情感分析。

(2)利用公开数据集,开展图像分类、恶意软件检测等

(3)利用Python库,基于OCR技术实现自动化提取图片中数据,并填入excel中。

(4)爬取天气数据,实现自动化微信提醒

(5)利用爬虫,实现自动化下载网站视频、文件等。

(6)编写小游戏:坦克大战、贪吃蛇、扫雷等等

注:在Windows/Linux系统上使用VIM、PDB、IDLE、Pycharm等工具编程实现。
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

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 = '

  • .?

    (.?)

    '
    tem2 = '.?<p title="(.*?)" class="wea"'
    tem3 = '

    .?(.?)'
    tem4 = '

    .?(.*?)'
    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 = '1157232747@qq.com'
    password = 'abc123456'
    tofxyyy = '1157232747@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)实验截图

    img

    3.实验中遇到的问题
    (1)未安装实验所需的requests库等数据库
    解决方法:在命令提示符中输入pip install requests来安装
    4.实验感悟
    这次实验的爬虫让我体会到了python的便捷性,可以快速收集整理数据。

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

110

社区成员

发帖
与我相关
我的任务
社区描述
人生苦短,我用Python!
python3.11 高校
社区管理员
  • blackwall0321
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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