参照bs4官方中文文档 http://beautifulsoup.readthedocs.org/en/latest/ 照着写了个简单的例子,感觉它非常强大!!
import urllib.request
from bs4 import BeautifulSoup
import sys
url = 'http://www.tantengvip.com'
html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html)
h1s = soup.find_all('h1',class_='entry-title')
'''打印所有H1标题和链接'''
for h1 in h1s:
#print(h1)
print(h1.get_text())
print(h1.a.attrs['href'])
结果:
D:\learn-python>python test.py
beautiful soul 4 for Python3中文文档
http://www.tantengvip.com/2015/05/beautiful-soul-4-for-pytho
Python文件名不能和已有模块名重名
http://www.tantengvip.com/2015/05/python-filename/
Python3 requests包抓取并保存网页源码
http://www.tantengvip.com/2015/05/requests-html/
Python作用域代码演示
http://www.tantengvip.com/2015/05/python-scope/
深入理解django url原理
http://www.tantengvip.com/2015/05/django-url/
使用Mac终端svn命令从SAE检出代码
http://www.tantengvip.com/2015/05/mac-svn-sae/
使用pip安装Django for Python3
http://www.tantengvip.com/2015/05/django-python3-install/
Python字典get方法小例子
http://www.tantengvip.com/2015/05/python-dict-get/
Python减少循环层次和缩进技巧
http://www.tantengvip.com/2015/05/python-for-level/
Python中new和init的区别
http://www.tantengvip.com/2015/05/python-new-init/
Python中requirements.pip文件的作用
http://www.tantengvip.com/2015/05/python-requirements-pip/
Python中name的含义
http://www.tantengvip.com/2015/05/python-name/
用python列举牌类游戏斗牛中所有牛牛的情况
http://www.tantengvip.com/2015/05/python-niu-niu/
ThinkPHP标签if condition嵌套bug
http://www.tantengvip.com/2015/05/thinkphp-if-condition-bug/
在SAE上部署django
http://www.tantengvip.com/2015/05/sae-django/
python中文基础教程
http://www.tantengvip.com/2015/05/python-guide-chinese/
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.