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运行的17个时新手常见错误小结
Aug 07 Python
python入门基础之用户输入与模块初认识
Nov 14 Python
全面了解django的缓存机制及使用方法
Jul 22 Python
django项目环境搭建及在虚拟机本地创建django项目的教程
Aug 02 Python
Python Web框架之Django框架Form组件用法详解
Aug 16 Python
python orm 框架中sqlalchemy用法实例详解
Feb 02 Python
pytorch masked_fill报错的解决
Feb 18 Python
Python实现链表反转的方法分析【迭代法与递归法】
Feb 22 Python
基于Python共轭梯度法与最速下降法之间的对比
Apr 02 Python
关于python 的legend图例,参数使用说明
Apr 17 Python
python实现学生成绩测评系统
Jun 22 Python
python使用scapy模块实现ARP扫描的过程
Jan 21 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中将指针移动到数据集初始位置的实现代码[mysql_data_seek]
2012/11/01 PHP
php常量详细解析
2015/10/27 PHP
PHP实现的统计数据功能详解
2016/12/06 PHP
IIS 7.5 asp Session超时时间设置方法
2017/04/17 PHP
php对象工厂类完整示例
2018/08/09 PHP
Laravel 框架路由原理与路由访问实例分析
2020/04/14 PHP
拉动滚动条加载数据的jquery代码
2012/05/03 Javascript
浅析document.createDocumentFragment()与js效率
2013/07/08 Javascript
一个很有趣3D球状标签云兼容IE8
2014/08/22 Javascript
Jquery实现由下向上展开效果的例子
2014/12/08 Javascript
详解Bootstrap glyphicons字体图标
2016/01/04 Javascript
JavaScript实现搜索框的自动完成功能(一)
2016/02/25 Javascript
老生常谈combobox和combotree模糊查询
2017/04/17 Javascript
Vue Autocomplete 自动完成功能简单示例
2019/05/25 Javascript
监控Nodejs的性能实例代码
2019/07/02 NodeJs
vue实现分页的三种效果
2020/06/23 Javascript
微信小程序地图实现展示线路
2020/07/29 Javascript
Vue 组件的挂载与父子组件的传值实例
2020/09/02 Javascript
跟老齐学Python之重回函数
2014/10/10 Python
Python实现蒙特卡洛算法小实验过程详解
2019/07/12 Python
详解numpy.meshgrid()方法使用
2019/08/01 Python
Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str
2019/08/07 Python
从numpy数组中取出满足条件的元素示例
2019/11/26 Python
基于python图像处理API的使用示例
2020/04/03 Python
python+adb命令实现自动刷视频脚本案例
2020/04/23 Python
python中sympy库求常微分方程的用法
2020/04/28 Python
python七种方法判断字符串是否包含子串
2020/08/18 Python
JD Sports澳洲官网:英国领先的运动鞋和运动时尚零售商
2020/02/15 全球购物
工厂总经理岗位职责
2014/02/07 职场文书
小学班主任寄语大全
2014/04/04 职场文书
毕业大学生自荐信
2014/06/17 职场文书
反邪教标语
2014/06/23 职场文书
商铺门面租房协议书
2014/10/21 职场文书
先进教师个人主要事迹材料
2015/11/03 职场文书
财务人员廉洁自律心得体会
2016/01/13 职场文书
争做文明公民倡议书
2019/06/24 职场文书