Редактирование: Python/FAQ/Работа с Web

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск
Внимание: Вы не представились системе. Ваш IP-адрес будет записан в историю изменений этой страницы.
Правка может быть отменена. Пожалуйста, просмотрите сравнение версий, чтобы убедиться, что это именно те изменения, которые вас интересуют, и нажмите «Записать страницу», чтобы изменения вступили в силу.
Текущая версия Ваш текст
Строка 3: Строка 3:
== Introduction ==
== Introduction ==
<source lang="python">
<source lang="python">
-
# @@INCOMPLETE@@
+
 
-
# @@INCOMPLETE@@
+
</source>
</source>
-
 
== Fetching a URL from a Perl Script ==
== Fetching a URL from a Perl Script ==
<source lang="python">
<source lang="python">
Строка 63: Строка 61:
             self._data[self._open_tags[-1]] = data
             self._data[self._open_tags[-1]] = data
     def __getattr__(self,attr):
     def __getattr__(self,attr):
-
         return self._data.get(attr, "")
+
         if not self._data.has_key(attr):
 +
            return ""
 +
 
 +
        return self._data[attr]
url = raw_url
url = raw_url
Строка 288: Строка 289:
title_s = re.search(r"(?i)<title>\s*(.*?)\s*</title>", text)
title_s = re.search(r"(?i)<title>\s*(.*?)\s*</title>", text)
-
title = title_s.group(1) if title_s or "NO TITLE"
+
title = title_s and title_s.groups()[0] or "NO TITLE"
# download the following standalone program
# download the following standalone program
Строка 316: Строка 317:
             self._data[self._open_tags[-1]] = data
             self._data[self._open_tags[-1]] = data
     def __getattr__(self,attr):
     def __getattr__(self,attr):
-
         return self._data.get(attr, "")
+
         if not self._data.has_key(attr):
 +
            return ""
 +
 
 +
        return self._data[attr]
     def error(self,msg):
     def error(self,msg):
         # ignore all errors
         # ignore all errors
Строка 372: Строка 376:
         if tag == 'a':
         if tag == 'a':
             if attrs[0][0] == 'href':
             if attrs[0][0] == 'href':
-
                 if ':' not in attrs[0][1]:
+
                 if attrs[0][1].find(':') == -1:
                     # we need to add the base URL.
                     # we need to add the base URL.
Строка 390: Строка 394:
data = s.read()
data = s.read()
p.feed(data)
p.feed(data)
-
for link in p.urls._data:
+
for link in p.urls._data.keys():
     state = "UNKNOWN URL"
     state = "UNKNOWN URL"
     if link.startswith("http:"):
     if link.startswith("http:"):
Строка 409: Строка 413:
import time
import time
import sys
import sys
-
from collections import defaultdict
 
-
Date = defaultdict(list)
+
Date = {}
-
while True:
+
while 1:
     # we only read from stdin not from argv.
     # we only read from stdin not from argv.
Строка 422: Строка 425:
         u = urllib.urlopen(ln)
         u = urllib.urlopen(ln)
         date = time.mktime(u.info().getdate("date"))
         date = time.mktime(u.info().getdate("date"))
 +
        if not Date.has_key(date):
 +
            Date[date] = []
         Date[date].append(ln)
         Date[date].append(ln)
     except:
     except:
Строка 441: Строка 446:
     text = open(filename).read()
     text = open(filename).read()
     def repl(matchobj):
     def repl(matchobj):
-
         return str(fillings.get(matchobj.group(1), ""))
+
         if fillings.has_key(matchobj.group(1)):
 +
            return str(fillings[matchobj.group(1)])
 +
        return ""
     # replace quoted words with value from fillings dictionary
     # replace quoted words with value from fillings dictionary
Строка 464: Строка 471:
     text = open(filename).read()
     text = open(filename).read()
     def repl(matchobj):
     def repl(matchobj):
-
         return str(fillings(matchobj.group(1), ""))
+
         if fillings.has_key(matchobj.group(1)):
 +
            return str(fillings[matchobj.group(1)])
 +
        return ""
     # replace quoted words with value from fillings dictionary
     # replace quoted words with value from fillings dictionary
Строка 471: Строка 480:
fields = cgi.FieldStorage()
fields = cgi.FieldStorage()
-
if "user" not in fields:
+
if not fields.has_key("user"):
     print "Content-Type: text/plain\n"
     print "Content-Type: text/plain\n"
Строка 504: Строка 513:
<source lang="python">
<source lang="python">
# @@INCOMPLETE@@
# @@INCOMPLETE@@
 +
# @@INCOMPLETE@@
# @@INCOMPLETE@@
</source>
</source>
-
 
== Parsing a Web Server Log File ==
== Parsing a Web Server Log File ==
<source lang="python">
<source lang="python">
Строка 555: Строка 564:
     f = split_re.match(line)
     f = split_re.match(line)
     if f:
     if f:
-
         print "agent = %s" % f.group('agent')
+
         print "agent = %s" % f.groupdict()['agent']
</source>
</source>
== Processing Server Logs ==
== Processing Server Logs ==
<source lang="python">
<source lang="python">
# @@INCOMPLETE@@
# @@INCOMPLETE@@
 +
# @@INCOMPLETE@@
# @@INCOMPLETE@@
-
</source>
 
 +
</source>
== Program: htmlsub ==
== Program: htmlsub ==
<source lang="python">
<source lang="python">
Строка 570: Строка 580:
</source>
</source>
 +
 
 +
 +
</source>
== Program: hrefsub ==
== Program: hrefsub ==
<source lang="python">
<source lang="python">
-
# @@INCOMPLETE@@
+
 
-
# @@INCOMPLETE@@
+
</source>
</source>
{{Python_Footer}}
{{Python_Footer}}

Пожалуйста, обратите внимание, что все ваши добавления могут быть отредактированы или удалены другими участниками. Если вы не хотите, чтобы кто-либо изменял ваши тексты, не помещайте их сюда.
Вы также подтверждаете, что являетесь автором вносимых дополнений, или скопировали их из источника, допускающего свободное распространение и изменение своего содержимого (см. Wiki.crossplatform.ru:Авторское право). НЕ РАЗМЕЩАЙТЕ БЕЗ РАЗРЕШЕНИЯ ОХРАНЯЕМЫЕ АВТОРСКИМ ПРАВОМ МАТЕРИАЛЫ!


Шаблоны, использованные на текущей версии страницы: