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的便捷性,可以快速收集整理数据。

...全文
300 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
内容概要:本文提出一种面向高柔性柔性作业车间的混合调度优化算法——元胞邻域遗传-随机重启爬山混合调度优化算法,该算法深度融合元胞自动机的局部搜索机制与遗传算法的全局寻优能力,并创新性地引入随机重启爬山策略以增强跳出局部最优的能力,从而有效应对高柔性车间环境中工序灵活、设备多样、约束复杂的调度挑战;通过构建精细化的数学模型,算法在满足工艺顺序、资源能力等多重约束的前提下,以最小化最大完工时间等为目标,显著提升了调度方案的质量与求解效率,相关方法已通过Matlab编程实现,支持仿真实验与性能验证,为复杂制造系统的智能调度提供了理论支撑与技术路径; 适合人群:具备一定编程基础,熟悉Matlab工具,从事智能制造、工业工程、自动化或运筹优化方向的研究生、科研人员及工程技术人员; 使用场景及目标:① 解决高柔性作业车间中的复杂任务调度问题;② 提升多工序、多设备、多约束条件下生产调度的优化性能;③ 为智能优化算法在工业场景中的融合应用提供参考案例与代码实现基础; 阅读建议:建议读者结合文中提到的智能优化算法背景知识进行系统学习,重点关注元胞邻域结构设计与遗传算法的融合机制,动手运行并调试提供的Matlab代码,通过仿真实验加深对算法收敛性与调度效果的理解。

110

社区成员

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

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