Discuss / Python / 我的作业

我的作业

Topic source

lubang03

#1 Created at ... [Delete] [Delete and Lock User]

天气信息是从yahoo api上拉下来的

from bs4 import BeautifulSoup import datetime

def get_weather(data):

weather = {}

soup = BeautifulSoup(data,'lxml')
weather['city'] = soup.find("yweather:location")["city"]
weather['country'] = soup.find("yweather:location")["country"]
todayDate = soup.find("yweather:condition")["date"].split(',')[0]
today = soup.find("yweather:forecast",day=todayDate)
weather['today'] = {'text':today['text'],'low':today['low'],'high':today['high']}

currentDay = datetime.date.today()
tommorrow = currentDay + datetime.timedelta(days=1)
tommorrowStr = datetime.datetime.strftime(tommorrow,'%d %b %Y')
tommorrowData = soup.find("yweather:forecast",date=tommorrowStr)

weather['tommorrow'] = {'text':tommorrowData['text'],'low':tommorrowData['low'],'high':tommorrowData['high']}

return weather

data = r'''<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2017-09-15T03:00:48Z" yahoo:lang="en-US"> <results> <channel> <yweather:units xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" distance="km" pressure="mb" speed="km/h" temperature="C"> </yweather:units> <title>Yahoo! Weather - Shanghai, Shanghai, CN</title> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2151849/</link> <description>Yahoo! Weather for Shanghai, Shanghai, CN</description> <language>en-us</language> <lastBuildDate>Fri, 15 Sep 2017 11:00 AM CST</lastBuildDate> <ttl>60</ttl> <yweather:location xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" city="Shanghai" country="China" region=" Shanghai"> </yweather:location> <yweather:wind xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" chill="79" direction="35" speed="51.50"> </yweather:wind> <yweather:atmosphere xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" humidity="81" pressure="34202.54" rising="0" visibility="25.91"> </yweather:atmosphere> <yweather:astronomy xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" sunrise="5:38 am" sunset="5:59 pm"> </yweather:astronomy> <image> <title>Yahoo! Weather</title> <width>142</width> <height>18</height> <link>http://weather.yahoo.com</link> <url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url> </image> <item> <title>Conditions for Shanghai, Shanghai, CN at 10:00 AM CST</title> <geo:lat xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">31.247709 </geo:lat> <geo:long xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">121.472618 </geo:long> <link>http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2151849/</link> <pubDate>Fri, 15 Sep 2017 10:00 AM CST</pubDate> <yweather:condition xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="23" date="Fri, 15 Sep 2017 10:00 AM CST" temp="25" text="Breezy"> </yweather:condition> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="23" date="15 Sep 2017" day="Fri" high="26" low="24" text="Breezy"> </yweather:forecast> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="23" date="16 Sep 2017" day="Sat" high="26" low="22" text="Breezy"> </yweather:forecast> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="34" date="17 Sep 2017" day="Sun" high="27" low="21" text="Mostly Sunny"> </yweather:forecast> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="18 Sep 2017" day="Mon" high="28" low="20" text="Partly Cloudy"> </yweather:forecast> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="19 Sep 2017" day="Tue" high="28" low="22" text="Partly Cloudy"> </yweather:forecast> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="47" date="20 Sep 2017" day="Wed" high="26" low="23" text="Scattered Thunderstorms"> </yweather:forecast> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="11" date="21 Sep 2017" day="Thu" high="26" low="22" text="Showers"> </yweather:forecast> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="22 Sep 2017" day="Fri" high="26" low="22" text="Partly Cloudy"> </yweather:forecast> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="28" date="23 Sep 2017" day="Sat" high="27" low="22" text="Mostly Cloudy"> </yweather:forecast> <yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="12" date="24 Sep 2017" day="Sun" high="27" low="23" text="Rain"> </yweather:forecast> <description><img src="http://l.yimg.com/a/i/us/we/52/23.gif"/&gt; <BR /> <b>Current Conditions:</b> <BR />Breezy <BR /> <BR /> <b>Forecast:</b> <BR /> Fri - Breezy. High: 26Low: 24 <BR /> Sat - Breezy. High: 26Low: 22 <BR /> Sun - Mostly Sunny. High: 27Low: 21 <BR /> Mon - Partly Cloudy. High: 28Low: 20 <BR /> Tue - Partly Cloudy. High: 28Low: 22 <BR /> <BR /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2151849/">Full Forecast at Yahoo! Weather</a> <BR /> <BR /> <BR /> </description> <guid isPermaLink="false"></guid> </item> </channel> </results> </query> '''

if name == "main": print(get_weather(data))

你好,我在复制你的程序到我电脑的pycharm中运行的时候,报错提示 bs4未找到,不知道这个模块是什么模块

Beautiful Soup4.3.2 个人理解解析Url 时需要导入的模块 可以在pip 安装一下。

请问一下为什么我跑你的代码没有输出结果


  • 1

Reply