Bra*_*rad 4 python beautifulsoup web-scraping
我正在尝试使用以下方法从天气现场刮取温度:
import urllib2
from BeautifulSoup import BeautifulSoup
f = open('airport_temp.tsv', 'w')
f.write("Location" + "\t" + "High Temp (F)" + "\t" + "Low Temp (F)" + "\t" + "Mean Humidity" + "\n" )
eventually parse from http://www.wunderground.com/history/airport/\w{4}/2012/\d{2}/1/DailyHistory.html
for x in range(10):
locationstamp = "Location " + str(x)
print "Getting data for " + locationstamp
url = 'http://www.wunderground.com/history/airport/KAPA/2013/3/1/DailyHistory.html'
page = urllib2.urlopen(url)
soup = BeautifulSoup(page)
location = soup.findAll('h1').text
locsent = location.split()
loc = str(locsent[3,6])
hightemp = soup.findAll('nobr')[6].text
htemp = hightemp.split()
ht = str(htemp[1])
lowtemp = soup.findAll('nobr')[10].text
ltemp = lowtemp.split()
lt = str(ltemp[1])
avghum = soup.findAll('td')[23].text
f.write(loc + "\t|" + ht + "\t|" + lt + "\t|" + avghum + "\n" )
f.close()
Run Code Online (Sandbox Code Playgroud)
不幸的是,我收到一个错误说:
Getting data for Location 0
Traceback (most recent call last):
File "airportweather.py", line 18, in <module>
location = soup.findAll('H1').text
AttributeError: 'list' object has no attribute 'text'
Run Code Online (Sandbox Code Playgroud)
我查看了BS和Python文档,但我仍然很绿,所以我无法理解.请帮助这个新手!
该.findAll()方法返回匹配列表.如果您想要一个结果,请改用该.find()方法.或者,选择一个特定元素,如代码的其余部分,或循环结果:
location = soup.find('h1').text
Run Code Online (Sandbox Code Playgroud)
要么
locations = [el.text for el in soup.findAll('h1')]
Run Code Online (Sandbox Code Playgroud)
要么
location = soup.findAll('h1')[2].text
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13403 次 |
| 最近记录: |