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 开发Activex组件方法
Nov 08 Python
重命名批处理python脚本
Apr 05 Python
Python画图学习入门教程
Jul 01 Python
python实现BackPropagation算法
Dec 14 Python
利用Python2下载单张图片与爬取网页图片实例代码
Dec 25 Python
python判断完全平方数的方法
Nov 13 Python
Python input函数使用实例解析
Nov 22 Python
使用Python脚本从文件读取数据代码实例
Jan 19 Python
PyCharm取消波浪线、下划线和中划线的实现
Mar 03 Python
python tkinter GUI绘制,以及点击更新显示图片代码
Mar 14 Python
python 实现关联规则算法Apriori的示例
Sep 30 Python
python简单实现插入排序实例代码
Dec 16 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
ip签名探针
2006/10/09 PHP
PHP中mysql_field_type()函数用法
2014/11/24 PHP
PHP SPL标准库之数据结构堆(SplHeap)简单使用实例
2015/05/12 PHP
PHP统计当前在线用户数实例讲解
2015/10/21 PHP
PHP实现的构造sql语句类实例
2016/02/03 PHP
PHP生成和获取XML格式数据的方法
2016/03/04 PHP
JAVASCRIPT函数作用域和提前声明 分享
2013/08/22 Javascript
file控件选择上传文件确定后触发的js事件是哪个
2014/03/17 Javascript
JavaScript用Number方法实现string转int
2014/05/13 Javascript
Javascript 读取操作Sql中的Xml字段
2014/10/09 Javascript
text-align:justify实现文本两端对齐 兼容IE
2015/08/19 Javascript
js实现黑色简易的滑动门网页tab选项卡效果
2015/08/31 Javascript
javascript中eval解析JSON字符串
2016/02/27 Javascript
jQuery 操作input中radio的技巧
2016/07/18 Javascript
JavaScript BASE64算法实现(完美解决中文乱码)
2017/01/10 Javascript
nodejs模块学习之connect解析
2017/07/05 NodeJs
详解Webpack DLL用法以及功能
2017/07/11 Javascript
BootStrap Table实现server分页序号连续显示功能(当前页从上一页的结束序号开始)
2017/09/12 Javascript
countUp.js实现数字滚动效果
2019/10/18 Javascript
vue实现自定义多选按钮
2020/07/16 Javascript
[01:26]神话结束了,却也刚刚开始——DOTA2新英雄玛尔斯驾临战场
2019/03/10 DOTA
详细介绍Python函数中的默认参数
2015/03/30 Python
Python爬虫实例_城市公交网络站点数据的爬取方法
2018/01/10 Python
解决phantomjs截图失败,phantom.exit位置的问题
2018/05/17 Python
postman和python mock测试过程图解
2020/02/22 Python
基于python连接oracle导并出数据文件
2020/04/28 Python
伊芙丽官方旗舰店:中国淑女一线品牌
2017/12/01 全球购物
Expedia挪威官网:酒店、机票和租车
2018/03/03 全球购物
医生自荐信
2013/10/11 职场文书
大学生专业个人学习的自我评价
2013/10/26 职场文书
十八届三中全会个人学习材料
2014/02/13 职场文书
会计员岗位职责
2014/03/15 职场文书
争先创优演讲稿
2014/09/15 职场文书
社保代办委托书怎么写
2014/10/06 职场文书
给校长的建议书作文400字
2015/09/14 职场文书
Python中可变和不可变对象的深入讲解
2021/08/02 Python