Python爬虫入門実戦------一週間天気予報爬取【転載】【分析なし】
4473 ワード
ソース:https://blog.csdn.net/qq_40705355/article/details/83856960
7日間の天気源:http://www.weather.com.cn/weather/101120101.shtml
ソースプログラム
効果図
自己分析:固定先頭
自己分析じこぶんせき:分析ぶんせき
エレメントを選択すると特に便利です.
7日間の天気源: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 ,
自己分析じこぶんせき:分析ぶんせき
エレメントを選択すると特に便利です.