次のプログラムを使用して、Web サイトから取得した都市のリストを作成しました。次に、作成したリストから都市の名前 (引数) を見つけたいと思います。それ、どうやったら出来るの?
つまり、リストからオブジェクトを見つけるにはどうすればよいでしょうか? 試してみました: listOfCities.find (city)
、属性検索が見つからなかったため、エラーが発生しました。
def weatherNow (city):
import urllib
connection = urllib.urlopen("http://weather.canoe.ca/Weather/World.html")
weather = connection.read()
connection.close()
cityLoc = weather.find('class="weatherred"')
cityEnd = weather.find("</a>", cityLoc)
if city != -1:
listOfCities = []
while cityLoc != -1:
cityNames = weather[cityLoc+19:cityEnd-1]
listOfCities.append(cityNames)
cityLoc = weather.find('class="weatherred"', cityLoc+1)
cityEnd = weather.find("</a>", cityLoc)
print listOfCities