代码拉取完成,页面将自动刷新
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
#import csv
def get_content(url, data=None):
rep = requests.get(url, timeout=60)
rep.encoding = 'utf-8'
return rep.text
def get_data(htmltext, city):
content = []
bs = BeautifulSoup(htmltext, "html.parser")
body = bs.body
data = body.find('div', {'id': '7d'})
ul = data.find('ul')
li = ul.find_all('li')
for day in li:
line = [city]
date = day.find('h1').string
line.append(date)
text = day.find_all('p')
line.append(text[0].string)
if text[1].find('span') is None:
temperature_H = None
else:
temperature_H = text[1].find('span').string.replace('℃', '')
temperature_L = text[1].find('i').string.replace('℃', '')
line.append(temperature_H)
line.append(temperature_L)
content.append(line)
return content
def save_data(data, filename):
with open(filename, 'a', errors='ignore', newline='') as f:
f_csv = csv.writer(f)
f_csv.writerows(data)
def get_url(city_name):
url = 'http://www.weather.com.cn/weather/'
with open('city.txt', 'r', encoding='UTF-8') as fs:
lines = fs.readlines()
for line in lines:
if(city_name in line):
code = line.split('=')[0].strip()
return url + code + '.shtml'
raise ValueError('invalid city name')
if __name__ == '__main__':
cities = input('city name: ').split(' ')
for city in cities:
url = get_url(city)
html = get_content(url)
result = get_data(html, city)
save_data(result, 'I:\weather.csv')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。