python爬虫入门实战---------一周天气预报爬取_Python爬虫入门实战--------一周天气预报爬取【转载】【没有分析...
Python爬蟲入門實戰--------一周天氣預報爬取【轉載】【沒有分析
Python爬蟲入門實戰--------一周天氣預報爬取【轉載】【沒有分析】
來源: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
''' 模擬瀏覽器,得到數據 '''
#更新為濟南的天氣網址
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() #將應答包里面的內容讀取出來
html = html.decode('utf-8') # 使用utf-8進行編碼,不重新編碼就會成亂碼
''' 模擬瀏覽器,得到數據 '''
final = [] #初始化一個空的list,我們為將最終的的數據保存到list
bs = BeautifulSoup(html,"html.parser") # 創建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標簽中的內容進行遍歷
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標簽中的內容(天氣狀況)加到temp中
if inf[1].find('span') is None:
temperature_highest = None # 天氣預報可能沒有當天的最高氣溫(到了傍晚,就是這樣),需要加個判斷語句,來輸出最低氣溫
else:
temperature_highest = inf[1].find('span').string # 找到最高溫度
temperature_highest = temperature_highest.replace('℃', '') # 到了晚上網站會變,最高溫度后面也有個℃
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
''' 模擬瀏覽器,得到數據 '''
#更新為濟南的天氣網址
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() #將應答包里面的內容讀取出來
html = html.decode('utf-8') # 使用utf-8進行編碼,不重新編碼就會成亂碼
自己分析:分析
選取元素 特別好用。
Python爬蟲入門實戰--------一周天氣預報爬取【轉載】【沒有分析相關教程
總結
以上是生活随笔為你收集整理的python爬虫入门实战---------一周天气预报爬取_Python爬虫入门实战--------一周天气预报爬取【转载】【没有分析...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3.7操作kafka_pyt
- 下一篇: 时间加减计算器_手机上的计算器这样也可以