#!/usr/bin/python
#weather.scraper
from bs4 import BeautifulSoup
import urllib
def main():
"""weather scraper"""
r = urllib.urlopen("https://www.wunderground.com/history/airport/KPHL/2016/1/1/MonthlyHistory.html?&reqdb.zip=&reqdb.magic=&reqdb.wmo=&MR=1").read()
soup = BeautifulSoup(r, "html.parser")
tables = soup.find_all("table", class_="responsive airport-history-summary-table")
scrapedData = {}
for table in tables:
print 'Weather Philadelphia'
for tr in table.find_all("tr"):
firstTd = tr.find("td")
if firstTd and firstTd.has_attr("class") and "indent" in firstTd['class']:
values = {}
tds = tr.find_all("td")
maxVal = tds[1].find("span", class_="wx-value")
avgVal = tds[2].find("span", class_="wx-value")
minVal = tds[3].find("span", class_="wx-value")
print maxVal, avgVal, minVal
if maxVal:
values['max'] = maxVal.text
if avgVal:
values['avg'] = avgVal.text
if minVal:
values['min'] = minVal.text
if len(tds) > 4:
sumVal = tds[4].find("span", class_="wx-value")
if sumVal:
values['sum'] = sumVal.text
scrapedData[firstTd.text] = values
print scrapedData
if __name__ == "__main__":
main()
は、それが何:このスクラッパーは私にいくつかのウェブサイト上の表の値を出力します。
私は、コードを実行すると、それは次のように私を出力します。
Weather Philadelphia
<span class="wx-value">18</span> <span class="wx-value">6</span> <span class="wx-value">-2</span>
<span class="wx-value">12</span> <span class="wx-value">1</span> <span class="wx-value">-6</span>
<span class="wx-value">6</span> <span class="wx-value">-3</span> <span class="wx-value">-11</span>
None None None
None None None
None None None
<span class="wx-value">14</span> <span class="wx-value">-7</span> <span class="wx-value">-21</span>
<span class="wx-value">35.6</span> <span class="wx-value">2.5</span> <span class="wx-value">0.0</span>
<span class="wx-value">46</span> <span class="wx-value">8</span> <span class="wx-value">0</span>
<span class="wx-value">61</span> <span class="wx-value">16</span> <span class="wx-value">0</span>
<span class="wx-value">79</span> <span class="wx-value">42</span> <span class="wx-value">27</span>
<span class="wx-value">1038</span> <span class="wx-value">1017</span> <span class="wx-value">993</span>
{u'Cooling Degree Days (base 65)': {}, u'Gust Wind': {'max': u'79', 'avg': u'42', 'min': u'27'}, u'Min Temperature': {'max': u'6', 'avg': u'-3', 'min': u'-11'}, u'Heating Degree Days (base 65)': {}, u'Dew Point': {'max': u'14', 'avg': u'-7', 'min': u'-21'}, u'Growing Degree Days (base 50)': {}, u'Snowdepth': {'max': u'46', 'avg': u'8', 'min': u'0'}, u'Sea Level Pressure': {'max': u'1038', 'avg': u'1017', 'min': u'993'}, u'Max Temperature': {'max': u'18', 'avg': u'6', 'min': u'-2'}, u'Precipitation': {'max': u'35.6', 'sum': u'66.80', 'avg': u'2.5', 'min': u'0.0'}, u'Wind': {'max': u'61', 'avg': u'16', 'min': u'0'}, u'Mean Temperature': {'max': u'12', 'avg': u'1', 'min': u'-6'}}
しかし、私は希望である代わりに:
<span class="wx-value">18</span> <span class="wx-value">6</span> <span class="wx-value">-2</span>
することは、スパンクラスせずに値を私をプリントアウトすることです
18
6
-2
ありがとうございます!
てみてくださいMAXVAL、avgVal、MINVAL
を印刷しようとしています。私は "justtext = scrapedData.get_text()"と同じ出力を得ます。 – malina
18 6 - 2 – cal97g
どういう意味ですか? "print scrapedData.strings()"? – malina