python爬虫(五)网页解析器

待我称王封你为后i 2021-09-27 04:36 509阅读 0赞

网页解析器:是从网页中提取有价值数据的工具
这里写图片描述
python 有四种网页解析器:
1 正则表达式:模糊匹配解析
2 html.parser:结构化解析
3 Beautiful Soup :结构化解析
4 lxml:结构化解析
其中 Beautiful Soup 功能很强大,有html.parse和 lxml的解析器.
结构化解析-DOM(Document Object Model)树

这里写图片描述

下载 beautifulSoup
pip install beautifulsoup4

这里写图片描述

beautifulSoup 语法:
这里写图片描述

其中find_all方法会搜索满足要求的所有节点
find方法只会搜索第一个满足要求的节点

节点的介绍:
这里写图片描述

一 创建beautifulSoup对象
这里写图片描述

二 搜索节点
这里写图片描述

其中beautifulSoup有个强大的功能是 可以传入正则表达式来匹配的内容.
class_ 这里加一个下划线是因为避免与python关键字冲突所以用一个下划线.

三 访问节点信息

这里写图片描述

实例测试:

  1. from bs4 import BeautifulSoup
  2. import re
  3. html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """
  4. soup=BeautifulSoup(html_doc,'html.parser',from_encoding='utf-8')
  5. print('获取所有链接')
  6. links=soup.find_all('a')
  7. for link in links :
  8. print (link.name, link['href'],link.get_text())
  9. print('获取Lacie链接')
  10. linknode=soup.find_all('a',href='http://example.com/lacie')
  11. for link in linknode :
  12. print (link.name, link['href'],link.get_text())
  13. print('正则匹配')
  14. linknode=soup.find_all('a',href=re.compile(r'ill'))
  15. for link in linknode :
  16. print (link.name, link['href'],link.get_text())
  17. print('获取P')
  18. pnode=soup.find_all('p',class_='title')
  19. for link in pnode :
  20. print (link.name,link.get_text())

学习自:慕课网.

发表评论

表情:
评论列表 (有 0 条评论,509人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Python爬虫网页解析难题

    在Python爬虫中,网页解析是一个非常关键和常见的难题。主要问题包括以下几个方面: 1. **HTML结构不稳定**:许多网站的HTML代码会经常变化,使得原有的解析方法失