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 正则表达式操作指南
May 04 Python
Python数据分析之真实IP请求Pandas详解
Nov 18 Python
Python数据拟合与广义线性回归算法学习
Dec 22 Python
使用python3+xlrd解析Excel的实例
May 04 Python
Python3 执行系统命令并获取实时回显功能
Jul 09 Python
python无序链表删除重复项的方法
Jan 17 Python
利用 Python ElementTree 生成 xml的实例
Mar 06 Python
python3中for循环踩过的坑记录
Dec 14 Python
python 实现逻辑回归
Dec 30 Python
Python爬取酷狗MP3音频的步骤
Feb 26 Python
详解Python中的进程和线程
Jun 23 Python
python的变量和简单数字类型详解
Sep 15 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
MySQL相关说明
2007/01/15 PHP
PHP中的生成XML文件的4种方法分享
2012/10/06 PHP
使用array mutisort 实现按某字段对数据排序
2013/06/18 PHP
ThinkPHP通过AJAX返回JSON的两种实现方法
2014/12/18 PHP
为你总结一些php信息函数
2015/10/21 PHP
php实现留言板功能(代码详解)
2017/03/28 PHP
PHP simplexml_load_file()函数讲解
2019/02/03 PHP
PHP的重载使用魔术方法代码实例详解
2021/02/26 PHP
在 IE 中调用 javascript 打开 Excel 表
2006/12/21 Javascript
javascript中的对象和数组的应用技巧
2007/01/07 Javascript
IE8 浏览器Cookie的处理
2009/01/31 Javascript
异步加载script的代码
2011/01/12 Javascript
Array的push与unshift方法性能比较分析
2011/03/05 Javascript
js图片轮播手动切换效果
2015/11/10 Javascript
javascript从定义到执行 你不知道的那些事
2016/01/04 Javascript
js编写当天简单日历效果【实现代码】
2016/05/03 Javascript
BootStrap导航栏问题记录
2017/07/31 Javascript
微信小程序之数据绑定原理解析
2019/08/14 Javascript
小程序实现左滑删除的效果的实例代码
2020/10/19 Javascript
[52:07]完美世界DOTA2联赛PWL S3 LBZS vs access 第二场 12.10
2020/12/13 DOTA
50行代码实现贪吃蛇(具体思路及代码)
2013/04/27 Python
详解使用python crontab设置linux定时任务
2016/12/08 Python
Python基础学习之常见的内建函数整理
2017/09/06 Python
python操作列表的函数使用代码详解
2017/12/28 Python
Python3.6实现连接mysql或mariadb的方法分析
2018/05/18 Python
python指定写入文件时的编码格式方法
2018/06/07 Python
mac下给python3安装requests库和scrapy库的实例
2018/06/13 Python
python自定义函数实现一个数的三次方计算方法
2019/01/20 Python
python mysql自增字段AUTO_INCREMENT值的修改方式
2020/05/18 Python
PyCharm最新激活码PyCharm2020.2.3有效
2020/11/18 Python
css3绘制天猫logo实现代码
2012/11/06 HTML / CSS
你可能不熟练的十个前端HTML5经典面试题
2018/07/03 HTML / CSS
施华洛世奇澳大利亚官网:SWAROVSKI澳大利亚
2017/01/06 全球购物
Linux的文件类型
2016/07/05 面试题
2014年大班元旦活动方案
2014/02/26 职场文书
插件导致ECharts被全量引入的坑示例解析
2022/09/23 Javascript