问题描述
我使用下面的程序来创建从一个网站获得的城市列表。现在我想从我创建的列表中找到城市的名称(参数)。我怎么做?换句话说,我如何从列表中找到一个对象?我试过:
检查 city 是否在 listOfCities $ c $
$ b $ pre $ 如果listOfCities中的城市:
#city在列表中
$ c
$ b
在列表中找到它的索引:
i = listOfCities.index(city)
如果city不在$中,则会引发 IndexError c $ c> listOfCities 。
您可以使用HTMLParser来解析html而不是正则表达式。
完整的示例
#!/ usr / bin / env python
# - * - coding: utf-8 - * -
from __future__ import unicode_literals
import cgi
try:
from html.parser import HTMLParser
除了ImportError:#Python 2
from HTMLParser import HTMLParser
try:$ b $ from urllib.request import urlopen
除了ImportError:#Python 2 $ b $ from urllib2 import urlopen
类CitiesParser(HTMLParser):
从html中提取城市列表。
def __init __(self,html):
HTMLParser .__ init __(self)
self.cities = []
self.incity = None
self.feed(html)
def handle_starttag(self,tag,attrs):
self .incity = tag =='a'和('c (self,tag):
self.incity = False
def handle_data(self,data):$ b $如果self.incity :
self.cities.append(data.strip())
#下载并解析城市列表
response = urlopen(http://weather.canoe.ca/ Weather / World.html)
_,params = cgi.parse_header(response.headers.get('Content-Type',''))
html = response.read()。decode(params ['charset'])
#找到城市
cities = CitiesParser(html).cities
在['ArRiyāḍ','Riyadh']:$ b $如果城市是城市:
print(%s is found%(city,))
print(index is%d%(cities.index(city),))
break
else:
print(%r not found%(city,))
I used the following program to create a list of cities obtained from a website. Now I want to find the name of city (argument) from the list I created. How do I do that?
In other words, how do I find an object from a list? I tried: listOfCities.find (city), I got an error as the attribute find was not found.
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
To check whether city is in listOfCities:
if city in listOfCities: # city is in the list
To find its index in the list:
i = listOfCities.index(city)
It raises IndexError if city is not in the listOfCities.
You could use HTMLParser to parse the html instead of regexes.
Complete example
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import unicode_literals import cgi try: from html.parser import HTMLParser except ImportError: # Python 2 from HTMLParser import HTMLParser try: from urllib.request import urlopen except ImportError: # Python 2 from urllib2 import urlopen class CitiesParser(HTMLParser): """Extract city list from html.""" def __init__(self, html): HTMLParser.__init__(self) self.cities = [] self.incity = None self.feed(html) def handle_starttag(self, tag, attrs): self.incity = tag == 'a' and ('class', 'weatherred') in attrs def handle_endtag(self, tag): self.incity = False def handle_data(self, data): if self.incity: self.cities.append(data.strip()) # download and parse city list response = urlopen("http://weather.canoe.ca/Weather/World.html") _, params = cgi.parse_header(response.headers.get('Content-Type', '')) html = response.read().decode(params['charset']) # find city cities = CitiesParser(html).cities for city in ['Ar Riyāḍ', 'Riyadh']: if city in cities: print("%s is found" % (city,)) print("the index is %d" % (cities.index(city),)) break else: print("%r is not found" % (city,))
这篇关于如何从列表中找到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!