b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

BeautifulSoup4的安装和简单使用

电脑杂谈  发布时间:2020-03-19 02:01:58  来源:网络整理

beautifulsoup怎么用_beautifulsoup python_beautifulsoup 中文

BeautifulSoup4的安装

Beautiful Soup 是用Python写的一个HTML/XML的解析器,它可以较好的处理不完善标记并生成分析树(parse tree)。

它提供简洁又常见的导航(navigating),搜索或者更改剖析树的操作。它可以大大节省你的编程时间。

一、使用pip直接安装beautifulsoup4

F:\>pip install beautifulsoup4

Collecting Beautifulsoup4

Downloading beautifulsoup4-4.4.1-py3-none-any.whl (81kB)

50% |████████████████ | 40kB 33kB/s eta 0:00:

62% |████████████████████▏ | 51kB 32kB/s eta

75% |████████████████████████▏ | 61kB 39kB/s

88% |████████████████████████████▏ | 71kB 21k

100% |████████████████████████████████| 81kB

25kB/s

beautifulsoup怎么用_beautifulsoup python_beautifulsoup 中文

Installing collected packages: Beautifulsoup4

Successfully installed Beautifulsoup4-4.4.1

或者从官方下载Beautifulsoup的硬件包,然后解压beautifulsoup怎么用,cmd命令行进入解压包目录,输入下面命令安装:python setup.py install

记得在Python3里一定要安装beautifulsoup4的版本beautifulsoup怎么用,其它版本安装不上的。

二、例子:

#python 3.4

#蔡军生 2016-6-13

#

from bs4 import BeautifulSoup

html_doc = """

<html><head><title>The Dormouse's story</title></head>

<body>

<p><b>The Dormouse's story</b></p>

beautifulsoup 中文_beautifulsoup怎么用_beautifulsoup python

<p>Once upon a time there were three little sisters; and their names were

<a href="http://example.com/elsie">Elsie</a>,

<a href="http://example.com/lacie">Lacie</a> and

<a href="http://example.com/tillie">Tillie</a>;

and they lived at the bottom of a well.</p>

<p>...</p>

"""

soup = BeautifulSoup(html_doc, "html.parser")

print(soup.title)

print('*' * 80)

print(soup.title.name)

print(soup.title.string)

print(soup.p)

beautifulsoup怎么用_beautifulsoup 中文_beautifulsoup python

print(soup.a)

print(soup.find_all('a'))

print(soup.find(id='link3'))

print(soup.get_text())

>>>

<title>The Dormouse's story</title>

********************************************************************************

title

The Dormouse's story

<p><b>The Dormouse's story</b></p>

<a href="http://example.com/elsie">Elsie</a>

[<a href="http://example.com/elsie">Elsie</a>, <a href="http://example.com/lacie">Lacie</a>, <a href="http://example.com/tillie">Tillie</a>]

<a href="http://example.com/tillie">Tillie</a>

beautifulsoup python_beautifulsoup怎么用_beautifulsoup 中文

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.

...

>>>

可以看出:soup 就是BeautifulSoup处理格式化后的字符串,soup.title 得到的是title标签,soup.p 得到的是文档中的第一个p标签,要想得到所有标签,得用find_all

函数。find_all 函数返回的是一个序列,可以对它进行循环,依次得到想到的东西.

get_text() 是返回文本,这个对每一个BeautifulSoup处理后的对象受到的标签都是生效的。你可以试试 print(soup.p.get_text())

其实是可以获得标签的其它属性的,比如我要获取a标签的href属性的值,可以使用 print(soup.a['href']),类似的其它属性,比如class也有可以这样得到的(soup.a['class'])。


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-144649-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...