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 相关文章推荐
Python3基础之基本运算符概述
Aug 13 Python
介绍Python中的文档测试模块
Apr 28 Python
简单介绍Python中的readline()方法的使用
May 24 Python
python 开发的三种运行模式详细介绍
Jan 18 Python
Tensorflow 自带可视化Tensorboard使用方法(附项目代码)
Feb 10 Python
Python使用import导入本地脚本及导入模块的技巧总结
Aug 07 Python
python多进程(加入进程池)操作常见案例
Oct 21 Python
Django框架HttpRequest对象用法实例分析
Nov 01 Python
为什么称python为胶水语言
Jun 16 Python
python解释器安装教程的方法步骤
Jul 02 Python
Django如何批量创建Model
Sep 01 Python
Python中快速掌握Data Frame的常用操作
Mar 31 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
SESSION信息保存在哪个文件目录下以及能够用来保存什么类型的数据
2012/06/17 PHP
使用YUI+Ant 实现JS CSS压缩
2014/09/02 PHP
PHP实现采集中国天气网未来7天天气
2014/10/15 PHP
php实现在限定区域里自动调整字体大小的类实例
2015/04/02 PHP
利用PHP获取汉字首字母并且分组排序详解
2017/10/22 PHP
PHP面向对象程序设计之构造方法和析构方法详解
2019/06/13 PHP
JavaScript中的继承方式详解
2015/02/11 Javascript
js实现类似菜单风格的TAB选项卡效果代码
2015/08/28 Javascript
js鼠标点击图片切换效果实现代码
2015/11/19 Javascript
React.js入门学习第一篇
2016/03/30 Javascript
jQuery页面元素动态添加后绑定事件丢失方法,非 live
2016/06/16 Javascript
jQuery层级选择器_动力节点节点Java学院整理
2017/07/04 jQuery
nodejs中sleep功能实现暂停几秒的方法
2017/07/12 NodeJs
Mobile Web开发基础之四--处理手机设备的横竖屏问题
2017/08/11 Javascript
React Native日期时间选择组件的示例代码
2018/04/27 Javascript
Swiper 4.x 使用方法(移动端网站的内容触摸滑动)
2018/05/17 Javascript
bootstrap table实现横向合并与纵向合并
2019/07/18 Javascript
nodemon实现Typescript项目热更新的示例代码
2019/11/19 Javascript
JavaScript字符和ASCII实现互相转换
2020/06/03 Javascript
JavaScript Image对象实现原理实例解析
2020/08/26 Javascript
Python使用minidom读写xml的方法
2015/06/03 Python
python发送邮件脚本
2018/05/22 Python
Python的条件表达式和lambda表达式实例
2019/01/31 Python
django基于restframework的CBV封装详解
2019/08/08 Python
django xadmin中form_layout添加字段显示方式
2020/03/30 Python
python Protobuf定义消息类型知识点讲解
2021/03/02 Python
你不知道的5个HTML5新功能
2016/06/28 HTML / CSS
飞利浦美国官网:Philips美国
2020/02/28 全球购物
New Balance比利时官方网站:购买鞋子和服装
2021/01/15 全球购物
如何写出好的Java代码
2014/04/25 面试题
计算机专业自我鉴定
2013/10/15 职场文书
决定成败的关键——创业计划书
2014/01/24 职场文书
股东合作协议书
2014/09/12 职场文书
2014年为民办实事工作总结
2014/12/20 职场文书
2015年度个人工作总结报告
2015/10/24 职场文书
社区志愿者服务心得体会
2016/01/22 职场文书