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 相关文章推荐
Django的URLconf中使用缺省视图参数的方法
Jul 18 Python
浅谈python中set使用
Jun 30 Python
使用pip发布Python程序的方法步骤
Oct 11 Python
python 输出列表元素实例(以空格/逗号为分隔符)
Dec 25 Python
python和c语言哪个更适合初学者
Jun 22 Python
Python如何解除一个装饰器
Aug 07 Python
python中_del_还原数据的方法
Dec 09 Python
Django 实现图片上传和下载功能
Dec 31 Python
python pyg2plot的原理知识点总结
Feb 28 Python
python turtle绘制多边形和跳跃和改变速度特效
Mar 16 Python
宝塔更新Python及Flask项目的部署
Apr 11 Python
Python Matplotlib绘制等高线图与渐变色扇形图
Apr 14 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
SONY SRF-M100的电路分析
2021/03/02 无线电
PHP使用Alexa API获取网站的Alexa排名例子
2014/06/12 PHP
浅谈本地WAMP环境的搭建
2015/05/13 PHP
利用PHP将部分内容用星号替换
2020/04/21 PHP
总结PHP中数值计算的注意事项
2016/08/14 PHP
PHP实现的数独求解问题示例
2017/04/18 PHP
可以支持多中格式的JS键盘
2007/05/02 Javascript
prototype Element学习笔记(Element篇三)
2008/10/26 Javascript
javascript 用记忆函数快速计算递归函数
2010/03/15 Javascript
javascript面向对象之二 命名空间
2011/02/08 Javascript
JavaScript:new 一个函数和直接调用函数的区别分析
2013/07/10 Javascript
基于原生js淡入淡出函数封装(兼容IE)
2016/10/20 Javascript
JS取数字小数点后两位或n位的简单方法
2016/10/24 Javascript
面试常见的js算法题
2017/03/23 Javascript
vue 项目如何引入微信sdk接口的方法
2017/12/18 Javascript
vue注册组件的几种方式总结
2018/03/08 Javascript
vue+element-ui+ajax实现一个表格的实例
2018/03/09 Javascript
Vue中的Props(不可变状态)
2018/09/29 Javascript
ndm:NPM的桌面GUI应用程序
2018/10/15 Javascript
[jQuery] 事件和动画详解
2019/03/05 jQuery
详解js常用分割取字符串的方法
2019/05/15 Javascript
Vue+ElementUI 中级联选择器Bug问题的解决
2020/07/31 Javascript
[03:30]DOTA2完美“圣”典精彩集锦
2016/12/27 DOTA
python每次处理固定个数的字符的方法总结
2013/01/29 Python
python遍历数组的方法小结
2015/04/30 Python
python九九乘法表的实例
2017/09/26 Python
关于django 数据库迁移(migrate)应该知道的一些事
2018/05/27 Python
使用python制作一个为hex文件增加版本号的脚本实例
2019/06/12 Python
python中提高pip install速度
2020/02/14 Python
Django如何使用jwt获取用户信息
2020/04/21 Python
Python多线程:主线程等待所有子线程结束代码
2020/04/25 Python
Python基于callable函数检测对象是否可被调用
2020/10/16 Python
设置器与访问器的定义以及各自特点
2016/01/08 面试题
销售主管的自我评价分享
2014/01/03 职场文书
光学与应用专业毕业生求职信
2014/09/01 职场文书
体育部部长竞选稿
2015/11/21 职场文书