python爬虫学习笔记4:信息提取

信息组织与提取方法

信息标记的三种形式

xml

由HTML扩展而来的通用信息标记形式

实例

<person>
    <firstName>Tian</firstName>
    <lastName>Song</lastName>
    <address>
        <streeAddr>中关村南大街5号</streetAddr>
        <city>北京市</city>
        <zipcode>100081</zipcode>
    </address>
    <prof>Computer System</prof><prof>Security</prof>
<person>

扩展性好,但繁琐

json

有类型的键值对

"key":"value"
"key":["value1","value2"]
"key":{"subkey":"subvalue"}

实例

{
    "firstName":"Tian",
    "lastNmae":"Song",
    "address":{
        "streetAddr":"中关村南大街5号",
        "city":"北京市",
        "zipcode":"100081"
            },
    "prof":["Computer System","Security"]
}

适合程序处理(js),较xml简洁

YAML

无类型键值对 key:value

缩进表达所属关系,由|表达整块数据,#表示注释,-表示并列的值信息

key : value
key : #Comment
-value1
-value2
key :
    subkey : subvalue

text:|      #学校介绍
北京理工大学创立于……

实例

firstName : Tian
lastName : Song
address :
    atreeAddr :中关村南大街5号
    city:北京市
    zipcode:100081
prof :
-Computer System
-Security

可读性好

信息提取的一般方法

1.完整解析信息的标记形式,再提取关键信息
2.无视标记形式,直接搜索关键信息
3.融合1,2

基于bs4库的html内容查找方法

find_all()

<>.find_all(name,attrs,recursive,string,**kwargs)

返回一个列表类型,存储查找的结果

name:对标签名称的检索字符串
attrs:对标签属性值的检索字符串,可标注属性检索
recursive:是否对子孙全部检索,默认为True
string<>…</>中字符串区域的检索字符串

简写:
<tag>(…)等价于<tag>.find_all(…)
soup(…)等价于soup.find_all(…)

扩展方法

这里写图片描述

猜你喜欢

转载自blog.csdn.net/w0ryitang/article/details/80209624