日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python爬虫入门实战---------一周天气预报爬取_Python爬虫入门实战--------一周天气预报爬取【转载】【没有分析...

發(fā)布時間:2025/3/12 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬虫入门实战---------一周天气预报爬取_Python爬虫入门实战--------一周天气预报爬取【转载】【没有分析... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Python爬蟲入門實戰(zhàn)--------一周天氣預報爬取【轉(zhuǎn)載】【沒有分析

Python爬蟲入門實戰(zhàn)--------一周天氣預報爬取【轉(zhuǎn)載】【沒有分析】

來源:https://blog.csdn.net/qq_40705355/article/details/83856960

七天天氣來源:http://www.weather.com.cn/weather/101120101.shtml

源程序

import csv

import urllib.request

from bs4 import BeautifulSoup

''' 模擬瀏覽器,得到數(shù)據(jù) '''

#更新為濟南的天氣網(wǎng)址

url = "http://www.weather.com.cn/weather/101120101.shtml"header = ("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36") # 設置頭部信息

opener = urllib.request.build_opener() # 修改頭部信息

opener.addheaders = [header] #修改頭部信息

request = urllib.request.Request(url) # 制作請求

response = urllib.request.urlopen(request) # 得到請求的應答包

html = response.read() #將應答包里面的內(nèi)容讀取出來

html = html.decode('utf-8') # 使用utf-8進行編碼,不重新編碼就會成亂碼

''' 模擬瀏覽器,得到數(shù)據(jù) '''

final = [] #初始化一個空的list,我們?yōu)閷⒆罱K的的數(shù)據(jù)保存到list

bs = BeautifulSoup(html,"html.parser") # 創(chuàng)建BeautifulSoup對象

body = bs.body # 獲取body部分

data = body.find('div',{'id':'7d'}) # 找到id為7d的div

ul = data.find('ul') # 獲取ul部分

li = ul.find_all('li') # 獲取所有的li

# print (li)

i = 0

for day in li: # 對每個li標簽中的內(nèi)容進行遍歷

if i < 7:

temp = []

date = day.find('h1').string # 找到日期

# print (date)

temp.append(date) # 添加到temp中

# print (temp)

inf = day.find_all('p') # 找到li中的所有p標簽

# print(inf)

# print (inf[0])

temp.append(inf[0].string) # 第一個p標簽中的內(nèi)容(天氣狀況)加到temp中

if inf[1].find('span') is None:

temperature_highest = None # 天氣預報可能沒有當天的最高氣溫(到了傍晚,就是這樣),需要加個判斷語句,來輸出最低氣溫

else:

temperature_highest = inf[1].find('span').string # 找到最高溫度

temperature_highest = temperature_highest.replace('℃', '') # 到了晚上網(wǎng)站會變,最高溫度后面也有個℃

temperature_lowest = inf[1].find('i').string #找到最低溫度

temperature_lowest = temperature_lowest.replace('℃', '') # # 最低溫度后面有個℃,去掉這個符號

temp.append(temperature_highest)

temp.append(temperature_lowest)

final.append(temp)

i = i +1

# print(final)

with open('weather.csv', 'a', errors='ignore', newline='') as f:

f_csv = csv.writer(f)

f_csv.writerows(final)

效果圖

自己分析:固定開頭

import csv

import urllib.request

from bs4 import BeautifulSoup

''' 模擬瀏覽器,得到數(shù)據(jù) '''

#更新為濟南的天氣網(wǎng)址

url = "http://www.weather.com.cn/weather/101120101.shtml"

header = ("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36") # 設置頭部信息

opener = urllib.request.build_opener() # 修改頭部信息

opener.addheaders = [header] #修改頭部信息

request = urllib.request.Request(url) # 制作請求

response = urllib.request.urlopen(request) # 得到請求的應答包

html = response.read() #將應答包里面的內(nèi)容讀取出來

html = html.decode('utf-8') # 使用utf-8進行編碼,不重新編碼就會成亂碼

自己分析:分析

選取元素 特別好用。

Python爬蟲入門實戰(zhàn)--------一周天氣預報爬取【轉(zhuǎn)載】【沒有分析相關教程

總結(jié)

以上是生活随笔為你收集整理的python爬虫入门实战---------一周天气预报爬取_Python爬虫入门实战--------一周天气预报爬取【转载】【没有分析...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。