python使用BeautifulSoup分析网页信息的方法


Posted in Python onApril 04, 2015

本文实例讲述了python使用BeautifulSoup分析网页信息的方法。分享给大家供大家参考。具体如下:

这段python代码查找网页上的所有链接,分析所有的span标签,并查找class包含titletext的span的内容

#import the library used to query a website

import urllib2
#specify the url you want to query

url = "http://www.python.org"
#Query the website and return the html to the variable 'page'

page = urllib2.urlopen(url)
#import the Beautiful soup functions to parse the data returned from the website

from BeautifulSoup import BeautifulSoup
#Parse the html in the 'page' variable, and store it in Beautiful Soup format

soup = BeautifulSoup(page)
#to print the soup.head is the head tag and soup.head.title is the title tag

print soup.head

print soup.head.title
#to print the length of the page, use the len function

print len(page)
#create a new variable to store the data you want to find.

tags = soup.findAll('a')
#to print all the links

print tags
#to get all titles and print the contents of each title

titles = soup.findAll('span', attrs = { 'class' : 'titletext' })

for title in allTitles:

print title.contents

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python中函数的多种格式和使用实例及小技巧
Apr 13 Python
在Python中处理时间之clock()方法的使用
May 22 Python
python开发之函数定义实例分析
Nov 12 Python
python使用socket创建tcp服务器和客户端
Apr 12 Python
用python统计代码行的示例(包括空行和注释)
Jul 24 Python
Python调用服务接口的实例
Jan 03 Python
PyQt5 QTableView设置某一列不可编辑的方法
Jun 25 Python
Django连接数据库并实现读写分离过程解析
Nov 13 Python
Python读取csv文件实例解析
Dec 30 Python
Python+pyftpdlib实现局域网文件互传
Aug 24 Python
python的dict判断key是否存在的方法
Dec 09 Python
如何利用python实现列表嵌套字典取值
Jun 10 Python
python使用webbrowser浏览指定url的方法
Apr 04 #Python
用Python编写一个简单的俄罗斯方块游戏的教程
Apr 03 #Python
用Python代码来绘制彭罗斯点阵的教程
Apr 03 #Python
利用Python演示数型数据结构的教程
Apr 03 #Python
简洁的十分钟Python入门教程
Apr 03 #Python
初步解析Python中的yield函数的用法
Apr 03 #Python
几个提升Python运行效率的方法之间的对比
Apr 03 #Python
You might like
PHP实现的统计数据功能详解
2016/12/06 PHP
PHP+Ajax实现上传文件进度条动态显示进度功能
2018/06/04 PHP
超级强大的表单验证
2006/06/26 Javascript
JavaScript转换农历类实现及调用方法
2013/01/27 Javascript
js计算字符串长度包含的中文是utf8格式
2013/10/15 Javascript
基于javascript制作经典传统的拼图游戏
2016/03/22 Javascript
简单三步实现报表页面集成天气
2016/12/15 Javascript
微信小程序 支付功能开发错误总结
2017/02/21 Javascript
node跨域请求方法小结
2017/08/25 Javascript
详解vue-cli本地环境API代理设置和解决跨域
2017/09/05 Javascript
JS使用正则表达式找出最长连续子串长度
2017/10/26 Javascript
React通过父组件传递类名给子组件的实现方法
2017/11/13 Javascript
浅谈vue引入css,less遇到的坑和解决方法
2018/01/20 Javascript
原生JS实现图片懒加载之页面性能优化
2019/04/26 Javascript
[56:00]2018DOTA2亚洲邀请赛 4.6 淘汰赛 VP vs TNC 第二场
2018/04/10 DOTA
Python中的日期时间处理详解
2016/11/17 Python
Python对文件和目录进行操作的方法(file对象/os/os.path/shutil 模块)
2017/05/08 Python
Django添加feeds功能的示例
2018/08/07 Python
python调用百度语音REST API
2018/08/30 Python
Python制作动态字符图的实例
2019/01/27 Python
python elasticsearch环境搭建详解
2019/09/02 Python
Python中Flask-RESTful编写API接口(小白入门)
2019/12/11 Python
解决python3.6用cx_Oracle库连接Oracle的问题
2020/12/07 Python
Android本地应用打开方法——通过html5写连接
2016/03/11 HTML / CSS
Origins加拿大官网:雅诗兰黛集团高端植物护肤品牌
2017/11/19 全球购物
LEGO玩具英国官方商店:LEGO Shop GB
2018/03/27 全球购物
公司培训欢迎词
2014/01/10 职场文书
高中校园广播稿
2014/01/11 职场文书
行政助理的岗位职责
2014/02/18 职场文书
求职信标题怎么写
2014/05/26 职场文书
“四风”问题的主要表现和危害思想汇报
2014/09/19 职场文书
毕业论文答辩稿范文
2015/06/23 职场文书
教师网络培训心得体会
2016/01/09 职场文书
使用Django实现商城验证码模块的方法
2021/06/01 Python
开发者首先否认《遗弃》被取消的传言
2022/04/11 其他游戏
Python  序列化反序列化和异常处理的问题小结
2022/12/24 Python