python使用BeautifulSoup分析网页信息的方法

作者:令狐不聪 时间:2022-02-07 11:33:40 

本文实例讲述了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,BeautifulSoup,网页,信息
0
投稿

猜你喜欢

  • 2008农历新年各大网站Logo秀

    2008-02-11 16:33:00
  • js调用flash代码

    2010-01-23 12:35:00
  • 从xml中获取城市,省份名称

    2008-09-05 15:07:00
  • Python中的延迟绑定原理详解

    2022-07-08 16:18:35
  • python3 使用ssh隧道连接mysql的操作

    2023-09-01 19:15:11
  • 网页栅格系统研究(3):粒度问题

    2008-10-28 19:46:00
  • AJAX在GET中文的时候解决乱码的方法

    2007-11-04 13:04:00
  • php递归删除目录与文件的方法

    2023-09-12 02:02:02
  • php根据isbn书号查询amazon网站上的图书信息的示例

    2023-11-15 03:08:53
  • 使用Django和Postgres进行全文搜索的实例代码

    2022-07-06 10:52:15
  • 经典的退出浏览器弹窗代码

    2008-07-30 12:48:00
  • CSS3属性box-shadow图层阴影效果使用教程

    2010-05-16 14:54:00
  • 本地机apache配置基于域名的虚拟主机详解

    2023-11-17 07:40:37
  • Python爬虫之网络请求

    2023-10-29 21:28:08
  • python常见的占位符总结及用法

    2023-10-11 10:39:58
  • 使用Python脚本将文字转换为图片的实例分享

    2022-07-20 14:33:29
  • 多个版本的python共存时使用pip的正确做法

    2021-06-24 10:41:17
  • python简单程序读取串口信息的方法

    2024-01-02 02:42:18
  • python高效过滤出文件夹下指定文件名结尾的文件实例

    2023-08-03 13:55:13
  • pytorch 如何把图像数据集进行划分成train,test和val

    2023-12-26 15:28:10
  • asp之家 网络编程 m.aspxhome.com