#!/usr/bin/python3
# -*- coding: utf-8 -*-
from bs4 import BeautifulSouphtml
#參考https://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html#findAllNext%28name,%20attrs,%20text,%20limit,%20**kwargs%29%20and%20findNext%28name,%20attrs,%20text,%20**kwargs%29python
def helloworld():
print("hello world")
pass測試
if __name__ == '__main__':xml
xml = """<root>
<book class="bookname" herf="http://www.baidu.com">helloPython</book>
<book class="user">shz</book>
<a herf="http://www.baidu.com"/>
</root>"""
bf= BeautifulSoup(xml,"lxml")
print("==========================")
tag = bf.find(class_ = "bookname") #類型是tag
print("test: " + str(tag)) # <book class="bookname" herf="http://www.baidu.com">helloPython</book>
print("name: " + tag.name) # book
print("text: " + tag.text) # helloPython
print("attrs: " + str(tag.attrs))
print("attr:herf: " + str(tag["herf"]))
print("attr:class: " + str(tag["class"]))
print("==========================")
print("多個 返回list")
print("==========================")
tags= bf.findAll(name="book") #類型是tag
print("test: " + str(tags))
print("name: " + tags[1].name)
print("==========================")
print("多個 返回list")
print("==========================")
tags= bf.findAll(name="a") #類型是tag
print("test: " + str(tags))
print("name: " + tags[0].name)
print("herf: " + tags[0]["herf"])
pass
htm
以上是測試代碼utf-8
======================================================================it
下面是輸出結果io
==========================
test: <book class="bookname" herf="http://www.baidu.com">helloPython</book>
name: book
text: helloPython
attrs: {'herf': 'http://www.baidu.com', 'class': ['bookname']}
attr:herf: http://www.baidu.com
attr:class: ['bookname']
==========================
多個 返回list
==========================
test: [<book class="bookname" herf="http://www.baidu.com">helloPython</book>, <book class="user">shz</book>]
name: book
==========================
多個 返回list
==========================
test: [<a herf="http://www.baidu.com"></a>]
name: a
herf: http://www.baidu.com
class