`

HTML and URL Parser

 
阅读更多

HTMLParser是python用来解析html的模块。它可以分析出html里面的标签、数据等等,是一种处理html的简便途径。HTMLParser采用的是一种事件驱动的模式,当HTMLParser找到一个特定的标记时,它会去调用一个用户定义的函数,以此来通知程序处理。它主要的用户回调函数的命名都是以handler_开头的,都是HTMLParser的成员函数。当我们使用时,就从HTMLParser派生出新的类,然后重新定义这几个以handler_开头的函数即可。这几个函数包括:
handle_startendtag 处理开始标签和结束标签
handle_starttag 处理开始标签,比如<xx>
handle_endtag 处理结束标签,比如</xx>
handle_charref 处理特殊字符串,就是以&#开头的,一般是内码表示的字符
handle_entityref 处理一些特殊字符,以&开头的,比如 &nbsp;
handle_data 处理数据,就是<xx>data</xx>中间的那些数据
handle_comment 处理注释
handle_decl 处理<!开头的,比如<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
handle_pi 处理形如<?instruction>的东西
这里我以从网页中获取到url为例,介绍一下。要想获取到url,肯定是要分析<a>标签,然后取到它的href属性的值。下面是代码:

#-*-encoding:gb2312-*-
importHTMLParser

classMyParser(HTMLParser.HTMLParser):
def__init__(self):
HTMLParser.HTMLParser.
__init__(self)

defhandle_starttag(self,tag,attrs):
#这里重新定义了处理开始标签的函数
iftag=='a':
#判断标签<a>的属性
forname,valueinattrs:
ifname=='href':
printvalue


if__name__=='__main__':
a
='<html><head><title>test</title><body><ahref="http://www.163.com">链接到163</a></body></html>'

my
=MyParser()
#传入要分析的数据,是html的。
my.feed(a)
分享到:
评论

相关推荐

    HttpClient以及获取页面内容应用

    Creates a Parser object with the location of the resource (URL or file) You would typically create a DefaultHTMLParserFeedback object and pass it in. Parser(URLConnection connection) Construct a ...

    python3.6.5参考手册 chm

    The plistlib module: A Property-List Parser ctypes Enhancements Improved SSL Support Deprecations and Removals Build and C API Changes Port-Specific Changes: Windows Port-Specific Changes: Mac OS...

    web-scraper-and-excel-writer:一个Python脚本,它提取遍历网页列表的表的链接和名称。 最后将收集的数据转储到excel文件中

    怎么运行的使用urllib.request.urlopen(url)获取页面内容,然后使用解析html并将此参数作为features=html.parser 。 然后,我们就可以深入研究html内容并过滤所需的元素。 在这种情况下,该表具有唯一的标识符,因此...

    北大天网搜索引擎TSE源码

    * # integrate a new version linkparser provided by XIE Han * # according to all MD5 values of pages content, * for all the pages not seen before, store a new page * Updated: Nov 21 2003. ...

    netWindows_0.3.0_pre2

    dependency on blank.html removed * CSS-only themeing now available on all widgets * NW_getFileTxt() now supports same-page URL caching * resizeable components can now have minimum and maximum widths ...

    Python程序设计(第二版).chm

    More on HTML and URL Escapes Section 12.7. Sending Files to Clients and Servers Chapter 13. Larger Web Site Examples I Section 13.1. "Things to Do When Visiting Chicago" Section 13.2. The ...

    hibernate.properties

    ## select the classic query parser #hibernate.query.factory_class org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory ################# ### Platforms ### ################# ## JNDI ...

    id3:适用于Node和浏览器JavaScript ID3标签解析器

    可以从本地磁盘(仅节点),同源URL和File实例(HTML5 File API)读取文件。 用法 安装: $ npm i -S id3js AJAX 您可以通过URL解析远程MP3的ID3标签: &lt; script type =" module " &gt; import * as id3 from...

    XML轻松学习手册--XML肯定是未来的发展趋势,不论是网页设计师还是网络程序员,都应该及时学习和了解

    XML的parser发展为两类:一种是"非确认类paeser",只检测文档是否遵守XML语法规则,是否用元素标识建立了文档树。另一种是"确认类paeser",它不但检测文档语法,结构树,而且比较解析你使用的元素标识是否遵守了相应...

    wnetwrap:Wininet包装器-简单的https库

    using namespace std;int main(){req my_request; //GET method and firefox user agent used by defaultresp my_response = ... //very basic html parser }这会将GET请求发送到指定的URL并进行一些基本的解析以显示网

    EurekaLog_7.5.0.0_Enterprise

    15)..Added: Expanding environment variables for "Support URL" 16)..Fixed: Range-check error on systems with MBCS ACP 17)..Fixed: 64-bit shared memory manager may not work 18)..Fixed: Possible "Unit ...

    js使用小技巧

    URL=http://c98.yeah.net"&gt; 简单邮件 &lt;a href="mailto:aaa@bbb.com?subject=ccc&body=xxxyyy"&gt; 快速转到位置 obj.scrollIntoView(true) 锚 &lt;a name="first"&gt; &lt;a href="#first"&gt;anchors 网页传递参数 ...

    JavaEE 5.0 Tutorial.pdf

    UDDI and ebXML Standard Formats ....................................................................................... 52 Java EE Application Assembly andDeployment .....................................

    asp.net知识库

    从SQL中的一个表中导出HTML文件表格 获取數据库表的前N条记录 几段SQL Server语句和存储过程 生成表中的数据的脚本 最详细的SQL注入相关的命令整理 Oracle Oracle中PL/SQL单行函数和组函数详解 mssql+oracle Oracle...

Global site tag (gtag.js) - Google Analytics