Python实训day12am【网络爬虫大作业简略解析:动态生成html页面、数据写入Excel】
- Python實訓(xùn)-15天-博客匯總表
目錄
1、HTML頁面設(shè)計
2、生成每個城市的HTML頁面
2.1、HTML頁面代碼(weatherTemplate.html)
2.2、實例代碼-動態(tài)生成html頁面
3、使用DataFrame輸出數(shù)據(jù)到Excel中
3.1、Python獲取系統(tǒng)時間
3.2、實例代碼-DataFrame數(shù)據(jù)輸出到Excel中
1、HTML頁面設(shè)計
?
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>{}實時天氣</title><style type="text/css">body {width: 400px;margin: 50px auto;border: 3px solid skyblue;border-radius: 17px;}h2 {text-align: center;color: darkmagenta;}div {padding: 16px 17px;font-size: medium;background: azure;margin-bottom: 16px;}div p {font-weight: bold;display: inline-block;}</style> </head> <body><h2>{}實時天氣</h2><div><p>溫度:</p><span>{}℃</span></div><div><p>濕度:</p><span>{}</span></div><div><p>風(fēng)力:</p><span>{}</span></div><div><p>PM2.5:</p><span>{}</span></div> </body> </html>2、生成每個城市的HTML頁面
2.1、HTML頁面代碼(weatherTemplate.html)
python字符串format報錯KeyError的可能原因和解決辦法_HermitSun的博客-CSDN博客_format報錯keyerror
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>{}實時天氣</title><style type="text/css">body {{width: 400px;margin: 50px auto;border: 3px solid skyblue;border-radius: 17px;}}h2 {{text-align: center;color: darkmagenta;}}div {{padding: 16px 17px;font-size: medium;background: azure;margin-bottom: 16px;}}div p {{font-weight: bold;display: inline-block;}}</style> </head> <body><h2>{}實時天氣</h2><div><p>溫度:</p><span>{}℃</span></div><div><p>濕度:</p><span>{}</span></div><div><p>風(fēng)力:</p><span>{}</span></div><div><p>PM2.5:</p><span>{}</span></div> </body> </html>2.2、實例代碼-動態(tài)生成html頁面
demo3:利用以下demo,就可以生成所有城市的html天氣展示。
''' # demo3:利用以下demo,就可以生成所有城市的html天氣展示大作業(yè)題目思路引導(dǎo): 題目1:定時爬取每個地級市的實時天氣狀況、存入excel中、為每個城市生成html展示、歷史excel文件以每日為單位歸檔到文件夾中。 考察點:爬蟲+文件讀寫+目錄操作+pandas 網(wǎng)址:http://www.weather.com.cn/每個地市: 1.從網(wǎng)站上尋找所有的地市列表-->地市編碼(beautifulsoup解析頁面)或(找數(shù)據(jù)接口) 2.根據(jù)地市編號,爬取該地的實時天氣(beautifulsoup解析頁面)或(找數(shù)據(jù)接口)省份沒什么用,我們只需要地級市的信息。 ''' import os;# 利用以下demo,就可以生成所有城市的html天氣展示 ''' 通過爬取天氣網(wǎng)站得到的數(shù)據(jù)樣板demo(昨天-2022.1.18):['北京', '-1', '36%南風(fēng)', '2級', '78']['海淀', '0', '42%南風(fēng)', '1級', '69']['朝陽', '-7', '31%北風(fēng)', '3級', '23']['順義', '-1', '43%東南風(fēng)', '2級', '90']['懷柔', '-2', '46%西南風(fēng)', '2級', '87'] ''' demo = ['北京', '-4', '64%', '東方一級', '87']; # 樣例 root = r'C:\Users\lwx\Desktop\weather'; f = open(os.path.join(root, 'weatherTemplate.html'), 'r', encoding='utf-8') temp = f.read(); # 讀取模板內(nèi)容 f.close()# 生成HTML頁面 dir_html = r'C:\Users\lwx\Desktop\weather\實時天氣HTML頁面'; f = open(os.path.join(dir_html, demo[0] + '.html'), 'w', encoding='utf-8') # print(temp) # 打印HTML頁面代碼 content = temp.format(demo[0], demo[0], demo[1], demo[2], demo[3], demo[4]) f.write(content) f.close() print(demo[0],end="") print("——動態(tài)生成HTML頁面完畢!")3、使用DataFrame輸出數(shù)據(jù)到Excel中
Demo4:生成Excel文件。1.數(shù)據(jù)導(dǎo)入Excel;2.Excel文件的命名;3.文件夾的自動創(chuàng)建。
二維列表or字典 ——> DataFrame ——> Excel 。
3.1、Python獲取系統(tǒng)時間
python當(dāng)前日期時間_Python當(dāng)前日期時間_從零開始的教程世界-CSDN博客
Python日期格式化:python時間格式化 - Jaoany - 博客園
3.2、實例代碼-DataFrame數(shù)據(jù)輸出到Excel中
F:\Python38\python.exe F:/JetBrains/2pythonSpace/pythonCode/實訓(xùn)lwx作業(yè)/day12-am-02.py
2022-01-19
17時45分
Over!
F:/JetBrains/2pythonSpace/pythonCode/實訓(xùn)lwx作業(yè)/day12-am-02.py:43: FutureWarning: As the xlwt package is no longer maintained, the xlwt engine will be removed in a future version of pandas. This is the only engine in pandas that supports writing in the xls format. Install openpyxl and write to an xlsx file instead. You can set the option io.excel.xls.writer to 'xlwt' to silence this warning. While this option is deprecated and will also raise a warning, it can be globally set and the warning suppressed.
? df.to_excel(time_file, index=False); ?# False去除最左邊的一列
Process finished with exit code 0
''' # demo4:以時間日期命名文件夾并將數(shù)據(jù)存入Excel大作業(yè)題目思路引導(dǎo): 題目1:定時爬取每個地級市的實時天氣狀況、存入excel中、為每個城市生成html展示、歷史excel文件以每日為單位歸檔到文件夾中。 考察點:爬蟲+文件讀寫+目錄操作+pandas 網(wǎng)址:http://www.weather.com.cn/每個地市: 1.從網(wǎng)站上尋找所有的地市列表-->地市編碼(beautifulsoup解析頁面)或(找數(shù)據(jù)接口) 2.根據(jù)地市編號,爬取該地的實時天氣(beautifulsoup解析頁面)或(找數(shù)據(jù)接口)省份沒什么用,我們只需要地級市的信息。 ''' import pandas as pd from datetime import datetime import os# Demo4:生成Excel文件。1.數(shù)據(jù)導(dǎo)入Excel;2.Excel文件的命名;3.文件夾的自動創(chuàng)建。 # Demo4:以時間日期命名文件夾并將數(shù)據(jù)存入Excel # 二維列表or字典 ——> DataFrame ——> Exceldata = [['北京', '-4', '64%', '東方1級', '87'], ['海淀', '-2', '60%', '西南風(fēng)2級', '77']];df = pd.DataFrame(data, columns=['城市', '實時溫度', '濕度', '風(fēng)力', 'AQI']); # 行名稱root_excel = r'C:\Users\lwx\Desktop\weather\Excel_歸檔'; # df.excel(); # print(df.excel());day = str(datetime.date(datetime.now())); print(day) # 2022-01-19 time = str(datetime.time(datetime.now()))[:5].replace(":", "時") + '分'; print(time) # 17時18分# 歸檔 dir_day = os.path.join(root_excel, day); # 文件夾路徑 if not os.path.exists(dir_day): # 文件夾是否存在os.mkdir(dir_day); # 創(chuàng)建文件夾time_file = os.path.join(dir_day, time + '.xls'); # 生成Excel文件df.to_excel(time_file, index=False); # False去除最左邊的一列 print("Over!")?《蠱真人》
永生飄渺非我求,長存無為老愧羞。界壁消散亂世起,宿命一去競自由。
鷹擊長空鯨霸海,不試怎知龍與蚯。凡夫俗子豈識我,非到末路不甘休。?
矩為盤律為棋,世間萬法皆可尋。
初入江湖開魔道,三戰(zhàn)氣絕試鋒芒。
歲月忽晚生白發(fā),書山無盡少年華。
衍天化地窮神力,師法萬界道自然。?
?天行健,君子以自強不息。地勢坤,君子以厚德載物。——《周易》
大丈夫處世,若為酒色而忘本,此為禽獸何異?——《水滸傳》
佛說:“假使百千劫,所作業(yè)不亡,因緣會遇時,果報還自受。” 像這樣償還二、三百年前的業(yè)債,還要算是近的。
人必須有所不為,然后才能有所為。“不可”兩字之中,大有力量。——王克敬《不可不可錄》
道之呼吸——壹之形——柔聲細(xì)語
道之呼吸——貳之形——抑揚頓挫
道之呼吸——叁之形——理大勝雄辯
道之呼吸——肆之形——反其道而行
道之呼吸——伍之形——據(jù)理決不讓
道之呼吸——陸之形——公道自在人心
道之呼吸——柒之形——旁觀左道爭辯
道之呼吸——捌之形——反主為客道理真
道之呼吸——玖之形——非暴理不解爭端
道之呼吸——拾之形——大道無形,,,實在不行就上頭吧,不編了?。
總結(jié)
以上是生活随笔為你收集整理的Python实训day12am【网络爬虫大作业简略解析:动态生成html页面、数据写入Excel】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python实训day11pm【大作业简
- 下一篇: Python实训day12pm【答辩要求