MCPcopy Create free account
hub / github.com/ZiniuLu/Python-100-Days / main

Function main

Day66-75/code/example02.py:6–63  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4
5
6def main():
7 html = """
8 <!DOCTYPE html>
9 <html lang="en">
10 <head>
11 <meta charset="UTF-8">
12 <title>首页</title>
13 </head>
14 <body>
15 <h1>Hello, world!</h1>
16 <p>这是一个<em>神奇</em>的网站!</p>
17 <hr>
18 <div>
19 <h2>这是一个例子程序</h2>
20 <p>静夜思</p>
21 <p class="foo">床前明月光</p>
22 <p id="bar">疑似地上霜</p>
23 <p class="foo">举头望明月</p>
24 <div><a href="http://www.baidu.com"><p>低头思故乡</p></a></div>
25 </div>
26 <a class="foo" href="http://www.qq.com">腾讯网</a>
27 <img src="./img/pretty-girl.png" alt="美女">
28 <img src="./img/hellokitty.png" alt="凯蒂猫">
29 <img src="/static/img/pretty-girl.png" alt="美女">
30 <table>
31 <tr>
32 <th>姓名</th>
33 <th>上场时间</th>
34 <th>得分</th>
35 <th>篮板</th>
36 <th>助攻</th>
37 </tr>
38 </table>
39 </body>
40 </html>
41 """
42 soup = BeautifulSoup(html, 'lxml')
43 # JavaScript - document.title
44 print(soup.title)
45 # JavaScript - document.body.h1
46 print(soup.body.h1)
47 print(soup.p)
48 print(soup.body.p.text)
49 print(soup.body.p.contents)
50 for p_child in soup.body.p.children:
51 print(p_child)
52 print(len([elem for elem in soup.body.children]))
53 print(len([elem for elem in soup.body.descendants]))
54 print(soup.findAll(re.compile(r'^h[1-6]')))
55 print(soup.body.find_all(r'^h'))
56 print(soup.body.div.find_all(re.compile(r'^h')))
57 print(soup.find_all(re.compile(r'r$')))
58 print(soup.find_all('img', {'src': re.compile(r'\./img/\w+.png')}))
59 print(soup.find_all(lambda x: len(x.attrs) == 2))
60 print(soup.find_all(foo))
61 print(soup.find_all('p', {'class': 'foo'}))
62 for elem in soup.select('a[href]'):
63 print(elem.attrs['href'])

Callers 1

example02.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected