Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1755519
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Python/Ruby

2011-08-25 19:01:25

8.3. Extracting data from HTML documents

To extract data from HTML documents, subclass the SGMLParser class and define methods for each tag or entity you want to capture.

为了从html文档中提取数据,子类化类SGMLParser,然后对每一个你所打算捕捉的标签或是实体定义方法。

The first step to extracting data from an HTML document is getting some HTML. If you have some HTML lying around on your hard drive, you can use file functions to read it, but the real fun begins when you get HTML from live web pages.

html文件抽取数据的第一步是获取一些html文件。如果你的硬盘上面已经存在一些html文件,你可以使用文件函数来读入它,但是真正的乐趣是从获取真正的网页开始的。

Example 8.5. Introducing urllib

8.5 urllib 简介

  1. >>> import urllib
  2. >>> sock = urllib.urlopen("")
  3. >>> htmlSource = sock.read()
  4. >>> sock.close()
  5. >>> print htmlSource
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""><html><head>
  7.       <meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
  8.    <title>Dive Into Python</title>
  9. <link rel='stylesheet' href='diveintopython.css' type='text/css'>
  10. <link rev='made' href='mailto:mark@diveintopython.org'>
  11. <meta name='keywords' content='Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free'>
  12. <meta name='description' content='a free Python tutorial for experienced programmers'>
  13. </head>
  14. <body bgcolor='white' text='black' link='#0000FF' vlink='#840084' alink='#0000FF'>
  15. <table cellpadding='0' cellspacing='0' border='0' width='100%'>
  16. <tr><td class='header' width='1%' valign='top'>diveintopython.org</td>
  17. <td width='99%' align='right'><hr size='1' noshade></td></tr>
  18. <tr><td class='tagline' colspan='2'>Python&nbsp;for&nbsp;experienced&nbsp;programmers</td></tr>
  19.  
  20. [...snip...]

1

The urllib module is part of the standard Python library. It contains functions for getting information about and actually retrieving data from Internet-based URLs (mainly web pages).

Urllib模块是标准Python库的一部分。它包含从因特网上面获取信息以及检索信息的函数(主要是网页)。

2

The simplest use of urllib is to retrieve the entire text of a web page using the urlopen function. Opening a URL is similar to opening a file. The return value of urlopen is a file-like object, which has some of the same methods as a file object

Urllib最简单的使用方法就是使用urlopen方法检索出全本的文本一个网页上面的全部文本信息。打开一个URL同打开一个文件是类似的。Urlopen的返回值是一个同文件相类似的对象,它包含一些同文件对象相类似的方法。

3

The simplest thing to do with the file-like object returned by urlopen is read, which reads the entire HTML of the web page into a single string. The object also supportsreadlines, which reads the text line by line into a list.

对于urlopen函数返回到的同文件类似的对象最简单的操作就是读取它的内容,它将整个网页的html内容放到一个字符串中。该函数还支持readline,它一行一行的读取文本,然后将读到的文本存放在一个列表中。

4

When you're done with the object, make sure to close it, just like a normal file object

当你处理完该对象,确保你已经关闭它,就如对通常的文件一样。.

5

You now have the complete HTML of the home page of  in a string, and you're ready to parse it.

现在你已经有了 主页上网页内容的一份完全拷贝,你已经准备好去解析它了。

Example 8.6. Introducing urllister.py

8.6 urllister.py简介

If you have not already done so, you can  used in this book.

如果你还没有下载,你可以在现在本书中用到的所有代码。

  1. from sgmllib import SGMLParser
  2.  
  3. class URLLister(SGMLParser):
  4.     def reset(self):
  5.         SGMLParser.reset(self)
  6.         self.urls = []
  7.  
  8.     def start_a(self, attrs):
  9.         href = [v for k, v in attrs if k=='href']
  10.         if href:
  11.             self.urls.extend(href)

1

reset is called by the __init__ method of SGMLParser, and it can also be called manually once an instance of the parser has been created. So if you need to do any initialization, do it in reset, not in __init__, so that it will be re-initialized properly when someone re-uses a parser instance.

SGMLParser类的__init__方法调用了reset方法。它同样可以在一个解析类的实例在创建是手工创建。因此如果你想进行任何初始化,就可以在reset中完成,而不是在__init__,这样当有些人在再次使用解析类的实例时它就可以再次被合适的初始化。

2

start_a is called by SGMLParser whenever it finds an  tag. The tag may contain an href attribute, and/or other attributes, like name or title. The attrs parameter is a list of tuples, [(attributevalue), (attributevalue), ...]. Or it may be just an , a valid (if useless) HTML tag, in which case attrs would be an empty list.

SGMLparser类发现了一个标签时,它将调用start_a方法。该标签可能标签包含href属性,或是其它的属性,比如名字或是标题。Attrs参数是一个tuple类型的列表[(attribute,value),(attribute,value),…].同样它有可能近只是一个,一个合乎语法(如果没用任何用途)的html标签,在这情况下attrs将会是一个空列表。

3

You can find out whether this  tag has an href attribute with a simple multi-variable list comprehension.

你可以通过使用多个变量的列表综合操作来确定这个标签是否具有Href属性。

4

String comparisons like k=='href' are always case-sensitive, but that's safe in this case, because SGMLParser converts attribute names to lowercase while building attrs.

字符串在进行比较的时候,如k==’href’,通常是区分大小写的,但是在本例中这种比较是安全的,这是因为SGMLParser在构建attrs的时候会将属性名字转换成小写形式。

Example 8.7. Using urllister.py

8.7 使用urllister.py

  1. >>> import urllib, urllister
  2. >>> usock = urllib.urlopen("")
  3. >>> parser = urllister.URLLister()
  4. >>> parser.feed(usock.read())
  5. >>> usock.close()
  6. >>> parser.close()
  7. >>> for url in parser.urls: print url
  8. toc/index.html
  9. #download
  10. #languages
  11. toc/index.html
  12. appendix/history.html
  13. download/diveintopython-html-5.0.zip
  14. download/diveintopython-pdf-5.0.zip
  15. download/diveintopython-word-5.0.zip
  16. download/diveintopython-text-5.0.zip
  17. download/diveintopython-html-flat-5.0.zip
  18. download/diveintopython-xml-5.0.zip
  19. download/diveintopython-common-5.0.zip

... rest of output omitted for brevity ...

考虑到间接性,忽略其它的输出。

1

Call the feed method, defined in SGMLParser, to get HTML into the parser.[1] It takes a string, which is what usock.read() returns.

调用定义在SGMLParser中的feed方法,将html文档输入parser。它接受一个字符串,该字符串是usock.read()方法的返回值。

2

Like files, you should close your URL objects as soon as you're done with them.

同文件的操作类似,当你处理完,它们以后你应该关闭你的URL对象。

3

You should close your parser object, too, but for a different reason. You've read all the data and fed it to the parser, but the feed method isn't guaranteed to have actually processed all the HTML you give it; it may buffer it, waiting for more. Be sure to call close to flush the buffer and force everything to be fully parsed.

同样,你应该关闭你的parer对象,但是出于另一个不同的原因,你已经读取所有的数据并将它输出给了解析器,但是feed方法并不能保证实际上已经上处理了所有的你所给予的html文件:它可能缓冲它,等待更多的内容。请确保你已经调用close方法来刷洗缓冲从而强制所有的内容都被解析。

4

Once the parser is closed, the parsing is complete, and parser.urls contains a list of all the linked URLs in the HTML document. (Your output may look different, if the download links have been updated by the time you read this.)

一旦解析器被关闭,解析也就完成,parser.urls包含html文档中所有的外链URL的列表。(你的输出可能不同,假如你读取该文档的时候,文档中的外链已经被更新过)

阅读(1085) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~