本文共 1324 字,大约阅读时间需要 4 分钟。
BeautifulSoup模块是用来从HTML/XML等文件提取所需数据的Python库.,专为快速周转项目而设计,如屏幕抓取。三个功能使其功能强大:
(1)Beautiful Soup提供了一些简单的方法和Pythonic习语,用于导航,搜索和修改解析树:用于剖析文档和提取所需内容的工具包。编写应用程序不需要太多代码
(2)Beautiful Soup会自动将传入的文档转换为Unicode,将传出的文档转换为UTF-8。你不必考虑编码,除非文档未指定编码且Beautiful Soup无法检测到编码。然后你只需要指定原始编码。 (3)Beautiful Soup位于流行的Python解析器之上,如lxml和html5lib,允许尝试不同的解析策略或交易速度以获得灵活性。以管理员权限运行pip install beautifulsoup4
>>> import requests>>> r=requests.get("http://python123.io/ws/demo.html")>>> r.text'This is a python demo page \r\n\r\nThe demo python introduces several python courses.
\r\nPython is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\nBasic Python and Advanced Python.
\r\n'>>> demo =r.text>>> from bs4 import BeautifulSoup #bs4是beautifulsoup4库的简写,这里是在bs4 库里面导入一个BeautifulSoup类>>> soup=BeautifulSoup(demo,"html.parser") #html.parser解析器用于解析demo的html代码>>> print(soup.prettify())This is a python demo page The demo python introduces several python courses.
Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses: Basic Python and Advanced Python .
>>>
转载地址:http://astmb.baihongyu.com/