如何在<p>和</p> beautifulsoup之间打印数据

时间:2015-03-15 21:44:47

标签: python beautifulsoup

我正在编写一个脚本,以便在天气现场刮取天气的高低,我已经得到它来打印我需要的东西,但所有的Beautifulsoup标签。

这是我目前的剧本:

import urllib2
from bs4 import BeautifulSoup

website = "http://forecast.weather.gov/MapClick.php?lat=39.90489741058809&lon=-82.7617367885212&site=all&smap=1#.VPyDd4F4qAQ"

r1 = urllib2.urlopen(website)
mydata = r1.read()
soup = BeautifulSoup(mydata)
s = soup.prettify()
x = soup.find_all("p", attrs={"class": "point-forecast-icons-low"})
y = soup.find_all("p", attrs={"class": "point-forecast-icons-high"})

print x
print y

它给了我这个:

[<p class="point-forecast-icons-low">Low: 40 °F</p>, <p class="point-forecast-icons-low">Low: 48 °F</p>, <p class="point-forecast-icons-low">Low: 26 °F</p>, <p class="point-forecast-icons-low">Low: 31 °F</p>, <p class="point-forecast-icons-low">Low: 32 °F</p>]
[<p class="point-forecast-icons-high">High: 67 °F</p>, <p class="point-forecast-icons-high">High: 53 °F</p>, <p class="point-forecast-icons-high">High: 44 °F</p>, <p class="point-forecast-icons-high">High: 47 °F</p>]

但我只想要那些说&#34; High:##&#34;和#34;低:##。&#34;

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您有元素列表。在每个单独的元素上使用Element.text属性

lows = [low.text for low in soup.find_all("p", class_="point-forecast-icons-low")]
highs = [high.text for high in soup.find_all("p", class_="point-forecast-icons-high")]

这会产生:

>>> lows = [low.text for low in soup.find_all("p", class_="point-forecast-icons-low")]
>>> highs = [high.text for high in soup.find_all("p", class_="point-forecast-icons-high")]
>>> lows
[u'Low: 40 \xb0F', u'Low: 48 \xb0F', u'Low: 26 \xb0F', u'Low: 31 \xb0F', u'Low: 32 \xb0F']
>>> highs
[u'High: 67 \xb0F', u'High: 53 \xb0F', u'High: 44 \xb0F', u'High: 47 \xb0F']

°中的°F不是ASCII可打印字符,因此当在列表中显示时,它表示为\xb0转义序列。您可以打印单个值:

>>> print highs[0]
High: 67 °F