我有以下代碼:網絡,刮板,類打印純文本
#!/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
在此先感謝!
不幸的是不起作用。我得到的輸出與沒有「justtext = scrapedData.get_text()」相同的輸出「 – malina
嘗試.strings()on 18 6 - 2 – cal97g
你是怎麼說的。 「print scrapedData.strings()」? – malina