美文网首页程序员python数据分析人工智能机器学习工具癖
python数据提取工具beautifulsoup教程1快速入门

python数据提取工具beautifulsoup教程1快速入门

作者: python测试开发 | 来源:发表于2019-01-17 15:03 被阅读32次

简介

图片.png

Beautiful Soup是可以轻松从网页上抓取信息的库。 它位于HTML或XML解析器的顶部,提供Pythonic方式迭代,搜索和修改解析树。

安装

pip install -U beautifulsoup4

快速入门

图片.png
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup("<p>Some<b>bad<i>HTML", 'html.parser')
>>> print(soup.prettify())
<p>
 Some
 <b>
  bad
  <i>
   HTML
  </i>
 </b>
</p>
>>> soup.find(text="bad")
'bad'
>>> soup.i
<i>HTML</i>
>>> soup = BeautifulSoup("<tag1>Some<tag2/>bad<tag3>XML", "xml")
>>> print(soup.prettify())
<?xml version="1.0" encoding="utf-8"?>
<tag1>
 Some
 <tag2/>
 bad
 <tag3>
  XML
 </tag3>
</tag1>
>>> 

参考资料:https://pypi.org/project/beautifulsoup4/

参考资料

稍稍深入

图片.png

Beautiful Soup 可从HTML或XML文件中提取数据的Python库。通过你喜欢的转换器实现文档导航,查找,修改。.Beautiful Soup会帮你节省数小时或天的时间.

  • 加载文档

本节示例文档来自爱丽丝梦游仙境故事:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(html_doc, 'html.parser')
>>> print(soup.prettify())
<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 class="sister" href="http://example.com/elsie" id="link1">
    Elsie
   </a>
   ,
   <a class="sister" href="http://example.com/lacie" id="link2">
    Lacie
   </a>
   and
   <a class="sister" href="http://example.com/tillie" id="link3">
    Tillie
   </a>
   ;
and they lived at the bottom of a well.
  </p>
  <p class="story">
   ...
  </p>
 </body>
</html>
>>> 

html代码的形式:

<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>

浏览器打开的效果:

图片.png
  • 导航该数据结构的简单方法:
>>> soup.title
<title>The Dormouse's story</title>
>>> soup.title.name
'title'
>>> soup.title.string
"The Dormouse's story"
>>> soup.title.parent.name
'head'
>>> soup.p
<p class="title"><b>The Dormouse's story</b></p>
>>> soup.p['class']
['title']
>>> soup.a
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
>>> soup.find_all('a')
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
>>> soup.find(id="link3")
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>

常用操作:1,通过<a>标签找到所有链接。2,从页面中提取所有文本

>>> for link in soup.find_all('a'):
...     print(link.get('href'))
... 
http://example.com/elsie
http://example.com/lacie
http://example.com/tillie
>>> print(soup.get_text())

The Dormouse's story

The Dormouse's story
Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.
...

>>> 

相关文章

网友评论

    本文标题:python数据提取工具beautifulsoup教程1快速入门

    本文链接:https://www.haomeiwen.com/subject/eakidqtx.html