我正在尝试获取餐馆的名称、地址和电话号码。
我的代码一直卡在第二个定义中。第一个 def 工作正常。我不知道为什么,因为我无法识别任何错误。循环只是没有通过。
如果我犯了一个明显的错误,我会很感激有人发表评论。
谢谢
from urllib2 import urlopen
from csv import writer
def get_urls_of_restaurant():
list_urls = []
n = 0
nn = 0
for i in range(6):
url = urlopen('http://www.go.co.tz/index.php/restaurants/masaki?start=' + str(nn)).readlines() #open URL whis lists restaurants
while n < len(url):
if '<h2 class="contentheading">' in url[n]:
list_urls.append(url[n+1].split('"')[1])
n += 1
n = 0
nn += 3
list_urls.reverse()
print "Geting urls done! Get %s" %len(list_urls) + ' urls.'
return list_urls
def open_url_and_write_data(list_urls):
n = len(list_urls)-1
csv_file = open('restdar_guide.csv', 'wb')
file_writer = writer(csv_file, delimiter=';')
file_writer.writerow(['Name'] + ['address'] + ['phone'])
while n >= 0:
print 'Reading %s' % str(int(len(list_urls))-n) + " element of %s" % len(list_urls) + " element's..."
url = urlopen('http://www.go.co.tz' + list_urls[n]).readlines()
num_str = 0
list_write = []
while num_str < len(url):
if '<title>' in url[num_str]:
list_write.append(url[num_str].split('<')[0][7:])
if 'Location:</strong>' in url[num_str]:
list_write.append(url[num_str].split('<')[1][9:])
else:
list_write.append('unknown')
if '<li><strong>Tel:</strong>' in url[num_str]:
list_write.append(url[num_str].split('<')[2][10:])
else:
list_write.append('unknown')
file_writer.writerow([list_write[0]] + [list_write[1]] + [list_write[2]])
n -= 1
csv_file.close()
print 'Done!'
list_urls = get_urls_of_restaurant()
open_url_and_write_data(list_urls)