Python Development Lightweight Crawler 06

Source: Internet
Author: User

Python Development Lightweight Crawler (IMOOC summary 06--web parser)
介绍网页解析器    将互联网的网页获取到本地以后,我们需要对它们进行解析才能够提取出我们需要的内容。    也就是说网页解析器是从网页中提取有价值数据的一种工具,对于搜索引擎来说它会提取出网页所有的url,用于后续的访问。    但是,对于我们定向爬虫来说,除了提取出待爬取的url之外,会提取出我们感兴趣的想要的有价值的数据。    也就是说,网页解析器会以下载好的HTML网页字符串作为输入,然后提取出有价值的数据和新的待爬取的url列表。Python有哪几种网页解析器呢?    最直观的一种使用正则表达式,它会将整个网页当做一个字符串,然后使用模糊匹配方式来提取出有价值数据。    虽然直观,但是文档要是比较复杂的话,这种方式非常的麻烦。    我们可以使用python自带的html.parser来解析网页。    可以使用BeautifulSoup这个第三方插件来解析网页。    可以使用lxml这个第三方的插件来解析html网页或xml网页。    其中BeautifulSoup这个第三方插件,它可以使用python自带的html.Parser作为它的解析器,也可以用lxml作为它的解析器,相对来说比较强大。    这里使用BeautifulSoup来解析网页。    其中正则表达式是一种字符串型的模糊匹配,另外三种是结构化的解析。    什么是结构化的解析呢?    是将整个网页文档下载成一个DOM(document object model)树,    也就是document object model文档对象模型这么一棵树,以树的形式来进行元素的上下级遍历和访问的。

    DOM树是W3C官方定义的访问解析html的标准接口。    它会将整个网页的文档当成document对象,在它的下面就是html根节点,再下面就是head元素和body元素,head元素下面又会包含titile元素每个元素。    如果没有子元素,就会对应文本,比如说文本“我的标题”,Body元素下也会包含很多的子元素 a元素 div元素,a元素下包含需要文本链接文本 Div元素 包含段落文本。    同时每个元素有自身属性的话,herf就是链接元素它指定的url。    如何使用这种方式?    dom就将整个文档下载成树型结构,使用树型的上下级关系,可以很方便的定位到某个元素,然后访问这个元素的自身的属性、文本。    BeautifulSoup这个第三方的插件就是使用dom的方式来解析网页的。

Python Development Lightweight Crawler 06

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.