我对循环有疑问。我的问题是如何将输出写入文件。问题是我的FOR循环不会结束。你能帮我解决一下吗?
代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib2
import os
import sys
import re
import psycopg2
import time
import shutil
import logging
import glob
import copy
import urllib
import datetime
import requests
from lxml import html
import requests
import itertools
# Dir BACKUP
global current_backup
# Error log
global error_log
error_log = "rtc_error.log"
global current_time
current_time = time.strftime("%Y.%m.%d %H:%M", time.localtime())
def get_web():
try:
logging.basicConfig(level=logging.DEBUG, filename='{}'.format(error_log))
try:
input_sat = open('rtc.xml','w')
godina = datetime.date.today().strftime("%Y")
mjesec = datetime.date.today().strftime("%m")
for x in range (32):
if x < 32:
x = x + 1
var = x
url = 'http://www.rts.rs/page/tv/sr/broadcast/20/RTS+1.html?month={}&year={}&day={}&type=0'.format(mjesec, godina, var)
page = requests.get(url)
tree = html.fromstring(page.text)
a = tree.xpath('//h1/text()')
b = tree.xpath('//div[@class="ProgramTime"]/text()')
c = tree.xpath('//div[@class="ProgramName"]/text()')
d = tree.xpath('//a[@class="recnik"]/text()')
for line, line1, line2, line3 in itertools.product(a, b, c, d):
clean = line.strip(), line1.strip(), line2.strip(), line3.strip()
if clean:
print >> input_sat, line+line1+line2+line3
else:
break
#input_sat.write(line.encode('utf-8')) # the \n adds a line break
input_sat.close()
except:
logging.exception("Failed to parse web-page! %s"% current_time)
except:
logging.exception("Failed to parse web-page! %s"% current_time)
get_web()
我不知道问题在哪里,FOR LOOP或用编码字符解析。你能给我一些解决方案或更好的循环吗?
好的,我改成了这个循环:
for line in a:
clean = line.strip()
if clean:
print ''
for line1 in b:
clean1 = line1.strip()
if clean1:
print ''
for line2 in c:
clean2 = line2.strip()
if clean2:
#print line.encode('utf-8'),line1.encode('utf-8'),line2.encode('utf-8')
print >> input_sat, '<item>\n'+'<date>'+line.encode('utf-8')+'</date>\n'+'<start>'+line1.encode('utf-8')+'</start>\n'+'<title>'+line2.encode('utf-8')+'</title>\n'+'</item>\n'
但现在它写了多重播放值。