Beautifulsoup 库 -- 04 -- 修改文档树

x33g5p2x  于2021-09-19 转载在 其他  
字(5.1k)|赞(0)|评价(0)|浏览(512)

1. 修改文档树

Beautiful Soup 的强项是文档树的搜索,但同时也可以方便的修改文档树。

1.1 修改 tag 的名称和属性

  • 修改属性:
  1. soup = BeautifulSoup('<b class="boldest">Extremely bold</b>')
  2. tag = soup.b
  3. tag.name = "blockquote"
  4. tag['class'] = 'verybold'
  5. tag['id'] = 1
  6. print(tag)

输出:

  1. <blockquote class="verybold" id="1">Extremely bold</blockquote>
  • 删除属性:
  1. del tag['class']
  2. del tag['id']
  3. print(tag)

输出:

  1. <blockquote>Extremely bold</blockquote>

1.2 修改 string

  • 给 tag 的 string 属性赋值,就相当于用当前的内容替代了原来的内容:
  1. markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
  2. soup = BeautifulSoup(markup)
  3. tag = soup.a
  4. tag.string = "New link text."
  5. print(tag)

输出:

  1. <a href="http://example.com/">New link text.</a>

如果当前的 tag 包含了其它 tag,那么给它的 string 属性赋值会覆盖掉原有的所有内容包括子 tag。

1.3 append()

  • Tag.append() 方法向 tag 中添加内容,类似于 Python 的列表的 append() 方法:
  1. soup = BeautifulSoup("<a>Foo</a>")
  2. soup.a.append("Bar")
  3. print(soup)
  4. print(soup.a.contents)

输出:

  1. <html><head></head><body><a>FooBar</a></body></html>
  2. ['Foo','Bar']

1.4 NavigableString() 和 .new_tag()

  • 如果想添加一段文本内容到文档中也没问题,可以调用 Python 的 append() 方法或调用 NavigableString 的构造方法:
  1. soup = BeautifulSoup("<b></b>")
  2. tag = soup.b
  3. tag.append("Hello")
  4. new_string = NavigableString(" there")
  5. tag.append(new_string)
  6. print(tag)

输出:

  1. <b>Hello there.</b>
  • 创建一段注释:
  1. from bs4 import BeautifulSoup, Comment, NavigableString
  2. soup = BeautifulSoup("<b></b>")
  3. tag = soup.b
  4. tag.append("Hello")
  5. new_string = NavigableString(" there")
  6. tag.append(new_string)
  7. new_comment = soup.new_string("Nice to see you.", Comment)
  8. tag.append(new_comment)
  9. print(tag)

输出:

  1. <b>Hello there<!--Nice to see you.--></b>
  • Beautiful Soup 4.2.1 中新增的方法:

  • 创建一个 tag 最好的方法是调用工厂方法 BeautifulSoup.new_tag():

  1. from bs4 import BeautifulSoup
  2. soup = BeautifulSoup("<b></b>")
  3. tag_original = soup.b
  4. tag_new = soup.new_tag("a", href="http://www.example.com")
  5. tag_original.append(tag_new)
  6. print(tag_original)

输出:

  1. <b><a href="http://www.example.com"></a></b>

1.5 insert()

  • Tag.insert() 方法与 Tag.append() 方法类似;
  • 区别是不会把新元素添加到父节点 contents 属性的最后,而是把元素插入到指定的位置;
  • 与 Python 列表中的 insert() 方法的用法相同;
  1. from bs4 import BeautifulSoup
  2. markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
  3. soup = BeautifulSoup(markup)
  4. tag = soup.a
  5. print(tag)
  6. tag.insert(1, "but did not endorse")
  7. print(tag)
  8. print(tag.contents)

输出:

  1. <a href="http://example.com/">I linked to <i>example.com</i></a>
  2. <a href="http://example.com/">I linked to but did not endorse<i>example.com</i></a>
  3. ['I linked to ', 'but did not endorse', <i>example.com</i>]

1.6 insert_before() 和 insert_after()

  • insert_before() 方法在当前 tag 或文本节点前插入内容;
  1. from bs4 import BeautifulSoup
  2. soup = BeautifulSoup("<b>stop</b>")
  3. tag = soup.new_tag("a")
  4. tag.string = "Alice"
  5. soup.b.string.insert_before(tag)
  6. print(soup.b)

输出:

  1. <b><a>Alice</a>stop</b>
  • insert_after() 方法在当前 tag 或文本节点后插入内容;

1.7 clear()

  • Tag.clear() 方法移除当前 tag 的内容:
  1. markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
  2. soup = BeautifulSoup(markup)
  3. tag = soup.a
  4. tag.clear()
  5. print(tag)

输出:

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

1.8 extract()

  • PageElement.extract() 方法将当前 tag 移除文档树,并作为方法结果返回:
  1. from bs4 import BeautifulSoup
  2. markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
  3. soup = BeautifulSoup(markup)
  4. tag_a = soup.a
  5. tag_i = soup.i.extract()
  6. print(tag_a)
  7. print(tag_i)

输出:

  1. <a href="http://example.com/">I linked to </a>
  2. <i>example.com</i>
  • 这个方法实际上产生了 2 个文档树:

  • 一个是用来解析原始文档的 BeautifulSoup 对象;

  • 另一个是被移除并且返回的 tag;

  • 被移除并返回的 tag 可以继续调用 extract 方法;

1.9 decompose()

  • Tag.decompose() 方法将当前节点移除文档树并完全销毁;
  1. markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
  2. soup = BeautifulSoup(markup)
  3. a_tag = soup.a
  4. soup.i.decompose()
  5. print(a_tag)

输出:

  1. <a href="http://example.com/">I linked to</a>

1.10 replace_with()

  • replace_with() 方法移除文档树中的某段内容,并用新 tag 或文本节点替代它;
  1. markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
  2. soup = BeautifulSoup(markup)
  3. a_tag = soup.a
  4. new_tag = soup.new_tag("b")
  5. new_tag.string = "example.net"
  6. a_tag.i.replace_with(new_tag)
  7. print(a_tag)

输出:

  1. <a href="http://example.com/">I linked to <b>example.net</b></a>

replace_with() 方法返回被替代的 tag 或文本节点,可以用来浏览或添加到文档树其它地方。

1.11 wrap()

  • wrap() 方法可以对指定的 tag 元素进行包装,并返回包装后的结果。
  1. soup = BeautifulSoup("<p>I wish I was bold.</p>")
  2. print(soup.p.string.wrap(soup.new_tag("b")))
  3. print(soup.p.wrap(soup.new_tag("div")))

输出:

  1. <b>I wish I was bold.</b>
  2. <div><p><b>I wish I was bold.</b></p></div>

1.12 unwrap()

  • unwrap() 方法与 wrap() 方法相反;
  • 将移除 tag 内的所有 tag 标签,该方法常被用来进行标记的解包;
  1. markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
  2. soup = BeautifulSoup(markup)
  3. a_tag = soup.a
  4. a_tag.i.unwrap()
  5. print(a_tag)

输出:

  1. <a href="http://example.com/">I linked to example.com</a>

相关文章