#python #parsing #web #enumerate
#python #синтаксический анализ #веб #перечислять
Вопрос:
Предполагается, что следующий скрипт извлекает определенный номер строки и анализирует его с веб-сайта в реальном времени. Это работает примерно в течение 30 циклов, но затем кажется, что enumerate (f) перестает работать корректно… «i» в цикле for, похоже, останавливается на строке 130 вместо 200 с чем-то. Может ли это быть связано с веб-сайтом, с которого я пытаюсь получить данные, или с чем-то еще? Спасибо!!
import sgmllib
class MyParser(sgmllib.SGMLParser):
"A simple parser class."
def parse(self, s):
"Parse the given string 's'."
self.feed(s)
self.close()
def __init__(self, verbose=0):
"Initialise an object, passing 'verbose' to the superclass."
sgmllib.SGMLParser.__init__(self, verbose)
self.divs = []
self.descriptions = []
self.inside_div_element = 0
def start_div(self, attributes):
"Process a hyperlink and its 'attributes'."
for name, value in attributes:
if name == "id":
self.divs.append(value)
self.inside_div_element = 1
def end_div(self):
"Record the end of a hyperlink."
self.inside_div_element = 0
def handle_data(self, data):
"Handle the textual 'data'."
if self.inside_div_element:
self.descriptions.append(data)
def get_div(self):
"Return the list of hyperlinks."
return self.divs
def get_descriptions(self, check):
"Return a list of descriptions."
if check == 1:
self.descriptions.pop(0)
return self.descriptions
def rm_descriptions(self):
"Remove all descriptions."
self.descriptions.pop()
import urllib
import linecache
import sgmllib
tempLine = ""
tempStr = " "
tempStr2 = ""
myparser = MyParser()
count = 0
user = ['']
oldUser = ['none']
oldoldUser = [' ']
array = [" ", 0]
index = 0
found = 0
k = 0
j = 0
posIndex = 0
a = 0
firstCheck = 0
fCheck = 0
while a < 1000:
print a
f = urllib.urlopen("SITE")
a = a 1
for i, line in enumerate(f):
if i == 187:
print i
tempLine = line
print line
myparser.parse(line)
if fCheck == 1:
result = oldUser[0] is oldUser[1]
u1 = oldUser[0]
u2 = oldUser[1]
tempStr = oldUser[1]
if u1 == u2:
result = 1
else:
result = user is oldUser
fCheck = 1
user = myparser.get_descriptions(firstCheck)
tempStr = user[0]
firstCheck = 1
if result:
array[index 1] = array[index 1] 0
else:
j = 0
for z in array:
k = j 2
tempStr2 = user[0]
if k < len(array) and tempStr2 == array[k]:
array[j 3] = array[j 3] 1
index = j 2
found = 1
break
j = j 1
if found == 0:
array.append(tempStr)
array.append(0)
oldUser = user
found = 0
print array
elif i > 200:
print "HERE"
break
print array
f.close()
Ответ №1:
Возможно, количество строк на этой веб-странице меньше, чем вы думаете? Что это вам дает?:
print max(i for i, _ in enumerate(urllib.urlopen("SITE")))
Комментарии:
1. 564 строки. Что сбивает с толку, так это то, что он проходит через if i == 187 30-35 раз, а затем никогда больше… Я этого не понимаю
Ответ №2:
В сторону: ваш отступ заполнен после while a < 1000:
строки. Слишком много пустых строк и однобуквенных имен не способствуют пониманию вашего кода.
enumerate
не нарушено. Вместо таких предположений проверьте свои данные. Предложение: заменить
for i, line in enumerate(f):
Автор:
lines = list(f)
print "=== a=%d linecount=%d === % (a, len(lines))
for i, line in enumerate(lines):
print " a=%d i=%d line=%r" % (a, i, line)
Внимательно изучите выходные данные.