爬虫中信息的形式和提取(bs4中解析)


🌟信息组织与提取方法

(1) 🍉信息标记的三种形式

json

"key":"value"
"key":["value1","value2"]
"key":{"subkey":"subvalue"}
  • 信息有类型,适合程序处理(js),较XML简洁
  • 移动应用云端和节点的信息通信,无注释

yaml

key:vlaue
key:#Comment
-vlaue1
-vlaue2
key:
	subkey:subvlaue
  • 信息无类型,文本信息比例最高,可读性好
  • 各类系统的配置文件,有注释易读

xml

<name>……</name>
注释:<!-- -->
  • 最早的通用信息标记语言,可扩展性好,但繁琐。
  • Internet上的信息交互与传递

(2) 🍉信息提取的一般方法

  • 方法一:

    完整解析信息的标记形式,再提取关键信息。XML、JSON、YAML ,需要标记解析器;

    优点:信息解析准确;缺点:提取过程繁琐,速度慢。

  • 方法二:

    无视标记形式,直接搜索关键信息。对信息的文本查找函数即可。

    优点:提取过程简洁,速度较快。缺点:提取结果准确性与信息内容相关

  • 融合方法:

    结合形式解析与搜索方法,提取关键信息。XML、JSON、YAML、搜索。需要标记解析器及文本查找函数。

(3) 🍉find_all()方法

<>.find_all(name,attrs,recursive,string,**kwargs)
"""
返回一个列表类型,存储查找的结果
name:对标签名称的检索字符串
attrs:对标签属性值的检索字符串,可标注属性检索
recursive:是否对子孙全部检索,默认True
string:<>……</>中字符串区域的检索字符串
"""
  1. name:
import requests
from bs4 import BeautifulSoup
r=requests.get("http://python123.io/ws/demo.html").text
soup=BeautifulSoup(r,"html.parser")
# 单个标签查找
a=soup.find_all('a')
# 多个标签查找用列表
b=soup.find_all(['a','b'])
import requests
from bs4 import BeautifulSoup
r=requests.get("http://python123.io/ws/demo.html").text
soup=BeautifulSoup(r,"html.parser")
for tag in soup.find_all(True):
    print(tag.name)

# 返回结果
"""
html
head
title
body
p
b
p
a
a
"""
  1. attrs
import requests
from bs4 import BeautifulSoup
r=requests.get("http://python123.io/ws/demo.html").text
soup=BeautifulSoup(r,"html.parser")
# 属性的查找
link1=soup.find_all(id=['link1','class'])
print(link1)
p=soup.find_all('p','course')
print(p)
import requests
from bs4 import BeautifulSoup
import re
r=requests.get("http://python123.io/ws/demo.html").text
soup=BeautifulSoup(r,"html.parser")
# 利用正则表达式的函数compile搜索属性单词搜索
link=soup.find_all(id=re.compile('lin'))
print(link)
  1. recursive
import requests
from bs4 import BeautifulSoup
import re
r=requests.get("http://python123.io/ws/demo.html").text
soup=BeautifulSoup(r,"html.parser")
# 是否对子孙全部检索
a=soup.find_all('a',recursive=False)
print(a)
  1. string
import requests
from bs4 import BeautifulSoup
import re
r=requests.get("http://python123.io/ws/demo.html").text
soup=BeautifulSoup(r,"html.parser")
# 搜索字符串
str=soup.find_all(string='Basic Python')
print(str)
# 利用正则表达式搜索关键词
python=soup.find_all(string=re.compile('py'))
print(python)
  1. 拓展:
方法说明
<>.find()搜索且只返回一个结果,字符串类型,同.find_all参数
<>.find_parents()在先辈节点中搜索,返回列表类型,同.find_all参数
<>.find_parent()在先辈节点中返回一个结果,字符串类型,同.find_all参数
<>.find_next_siblings()在后续平行节点中搜索,返回列表类型,同.find_all参数
<>.find_next_sibling()在后续平行节点中返回一个结果,字符串类型,同.find_all参数
<>.find_previous_siblings()在前序平行节点中搜索,返回列表类型,同.find_all参数
<>.find_previous_sibling()在前序平行节点中返回一个结果,字符串类型,同.find_all参数

提取属性的方法:

import requests
from bs4 import BeautifulSoup
r=requests.get("http://python123.io/ws/demo.html").text
soup=BeautifulSoup(r,"html.parser")
for link in soup.find_all('a'):
    """
    提取属性的三种方法
    """
    print(link['href'])
    print(link.get('href'))
    print(link.attrs['href'])

:soup(…) 等价于 soup.find_all(…)

  • 11
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知道写什么的作者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值