Редактирование: Python/FAQ/Работа с Web
Материал из Wiki.crossplatform.ru
Внимание: Вы не представились системе. Ваш IP-адрес будет записан в историю изменений этой страницы.
Правка может быть отменена. Пожалуйста, просмотрите сравнение версий, чтобы убедиться, что это именно те изменения, которые вас интересуют, и нажмите «Записать страницу», чтобы изменения вступили в силу.
Текущая версия | Ваш текст | ||
Строка 3: | Строка 3: | ||
== Introduction == | == Introduction == | ||
<source lang="python"> | <source lang="python"> | ||
- | + | ||
- | + | ||
</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): | ||
- | + | 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. | + | 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): | ||
- | + | 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 | + | 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 | ||
- | |||
- | Date = | + | Date = {} |
- | while | + | 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 | + | 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 | + | 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" | + | 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. | + | 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> | ||
== 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"> | ||
- | + | ||
- | + | ||
</source> | </source> | ||
{{Python_Footer}} | {{Python_Footer}} |