﻿from bs4 import BeautifulSoup as bs
'''
	HomePage: http://www.crummy.com/software/BeautifulSoup/
	Download: http://www.crummy.com/software/BeautifulSoup/bs4/download/
	Documentation: http://www.crummy.com/software/BeautifulSoup/bs4/doc/
	Doc Chinese: http://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html
'''

html_doc = """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>Sample</title>
</head>
<body>
	<a href="http://www.google.com/">google<b>bold</b></a>
	<a href="http://www.baidu.com/" class="baidu">baidu</a>
	<a href="http://cn.bing.com/">bing</a>
	<a href="http://weibo.com/">weibo</a>
	<a href="http://t.qq.com/">tqq</a>
	<div class="container main">
		<a href="http://www.google.com/index.php" name="google">google index<b>bold</b></a>
		<a href="http://www.baidu.com/index.php">baidu index</a>
		<a href="http://cn.bing.com/index.php">bing index</a>
		<a href="http://weibo.com/index.php">weibo index</a>
		<a href="http://t.qq.com/index.php">tqq index</a>
	</div>
</body>
</html>"""

bs_doc = bs(html_doc)
for a in bs_doc.find_all('a'):
	print(a.get('href'))
	print(a['href'])
	print(a.text)
	print(a.get_text())
	print(a.string)
a = bs_doc.find('a')
print(a) # <a href="http://www.google.com/">google</a>

@@ -31,16 +31,27 @@
  as bs%0D%0A
+import re%0D%0A
 '''%0D%0A%09Ho
@@ -1045,16 +1045,30 @@
 dex.php%22
+ class=%22baidu%22
 %3Ebaidu i
@@ -1117,16 +1117,26 @@
 dex.php%22
+ id=%22bing%22
 %3Ebing in
@@ -1182,16 +1182,27 @@
 dex.php%22
+ id=%22weibo%22
 %3Eweibo i
@@ -1248,16 +1248,33 @@
 dex.php%22
+ data-foo=%22value%22
 %3Etqq ind
@@ -1540,12 +1540,946 @@
 google%3C/a%3E%0D%0A
+%0D%0Afor tag in bs_doc.find_all(re.compile(%22%5Eb%22)):%0D%0A    print(tag.name)%0D%0Aprint('*' * 80)%0D%0A%0D%0Afor tag in bs_doc.find_all(%5B'a', 'b'%5D):%0D%0A%09print(tag.name)%0D%0A%0D%0Aprint('*' * 80)%0D%0A%0D%0Afor tag in bs_doc.find_all(%22a%22, %22baidu%22):%0D%0A%09print(tag)%0D%0A%0D%0Aprint('*' * 80)%0D%0A%0D%0Afor tag in bs_doc.find_all(id = %22bing%22):%0D%0A%09print(tag)%0D%0A%0D%0Aprint('*' * 80)%0D%0A%0D%0Afor tag in bs_doc.find_all(id = True):%0D%0A%09print(tag)%0D%0A%0D%0Aprint('*' * 80)%0D%0A%0D%0Afor tag in bs_doc.find_all('a', id='weibo'):%0D%0A%09print(tag)%0D%0A%09%0D%0Aprint('*' * 80)%0D%0A%0D%0Afor tag in bs_doc.find_all(href=re.compile(%22baidu%22), attrs=%7B'class': 'baidu'%7D):%0D%0A%09print(tag)%0D%0A%0D%0Aprint('*' * 80)%0D%0A%0D%0Afor tag in bs_doc.find_all(%22a%22, class_=%22baidu%22):%0D%0A%09print(tag)%0D%0A%0D%0Aprint('*' * 80)%0D%0A%0D%0Afor tag in bs_doc.find_all(class_=%22container%22):%0D%0A%09print(tag)%0D%0A%0D%0Aprint('*' * 80)%0D%0A%0D%0Afor tag in bs_doc.find_all(class_=%22container main%22):%0D%0A%09print(tag)%0D%0A%0D%0Aprint('*' * 80)%0D%0A%0D%0Afor tag in bs_doc.select(%22div.container a.baidu%22):%0D%0A%09print(tag)%0D%0A%0D%0Aprint('*' * 80)%0D%0A%0D%0A%09

complete BeautifulSoup sample