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使用scrapy采集数据过程中放回下载过大页面的方法
Apr 08 Python
python开发之str.format()用法实例分析
Feb 22 Python
Python 给某个文件名添加时间戳的方法
Oct 16 Python
对python产生随机的二维数组实例详解
Dec 13 Python
python实现两张图片的像素融合
Feb 23 Python
python 计算数据偏差和峰度的方法
Jun 29 Python
使用Python开发个京东上抢口罩的小实例(仅作技术研究学习使用)
Mar 10 Python
Django之富文本(获取内容,设置内容方式)
May 21 Python
python相对企业语言优势在哪
Jun 12 Python
Python爬虫爬取微信朋友圈
Aug 06 Python
win10+anaconda安装yolov5的方法及问题解决方案
Apr 29 Python
PyCharm 配置SSH和SFTP连接远程服务器
May 11 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发送短信代码分享
2015/08/11 PHP
PHP实现自动识别原编码并对字符串进行编码转换的方法
2016/07/13 PHP
Avengerls vs Newbee BO3 第三场2.18
2021/03/10 DOTA
Jquery Ajax学习实例 向页面发出请求,返回XML格式数据
2010/03/14 Javascript
JavaScript基础篇之变量作用域、传值、传址的简单介绍与实例
2013/06/29 Javascript
使用Angular和Nodejs、socket.io搭建聊天室及多人聊天室
2015/08/21 NodeJs
javascript判断复选框是否选中的方法
2015/10/16 Javascript
Bootstrap前端开发案例一
2016/06/17 Javascript
功能强大的Bootstrap组件(结合js)
2016/08/03 Javascript
基于SpringMVC+Bootstrap+DataTables实现表格服务端分页、模糊查询
2016/10/30 Javascript
浅述Javascript的外部对象
2016/12/07 Javascript
使用ES6语法重构React代码详解
2017/05/09 Javascript
浅谈ng-zorro使用心得
2018/12/03 Javascript
vuex根据不同的用户权限展示不同的路由列表功能
2019/09/20 Javascript
JavaScript修改注册表实例代码
2020/01/05 Javascript
关于vue3默认把所有onSomething当作v-on事件绑定的思考
2020/05/15 Javascript
uniapp与webview之间的相互传值的实现
2020/06/29 Javascript
Node.js利用Express实现用户注册登陆功能(推荐)
2020/10/26 Javascript
Python 通配符删除文件的实例
2018/04/24 Python
Python格式化输出%s和%d
2018/05/07 Python
Python 使用Numpy对矩阵进行转置的方法
2019/01/28 Python
我就是这样学习Python中的列表
2019/06/02 Python
python爬虫 爬取58同城上所有城市的租房信息详解
2019/07/30 Python
Python实现自动访问网页的例子
2020/02/21 Python
如何利用python正则表达式匹配版本信息
2020/12/09 Python
explicit和implicit的含义
2012/11/15 面试题
《生命的药方》教学反思
2014/04/08 职场文书
初中优秀班集体申报材料
2014/05/01 职场文书
贪污受贿检讨书范文
2014/11/19 职场文书
2014年银行个人工作总结
2014/12/05 职场文书
办公室文员岗位职责
2015/02/04 职场文书
培根随笔读书笔记
2015/07/01 职场文书
Python趣味挑战之给幼儿园弟弟生成1000道算术题
2021/05/28 Python
yyds什么意思?90后已经听不懂00后讲话了……
2022/02/03 杂记
详解jQuery的核心函数和事件处理
2022/02/18 jQuery
win10电脑老是死机怎么办?win10系统老是死机的解决方法
2022/08/05 数码科技