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程序设计有所帮助。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
2008农历新年各大网站Logo秀
![](https://img.aspxhome.com/file/UploadPic/20082/11/200821117040777s.jpg)
js调用flash代码
从xml中获取城市,省份名称
Python中的延迟绑定原理详解
python3 使用ssh隧道连接mysql的操作
网页栅格系统研究(3):粒度问题
![](https://img.aspxhome.com/file/UploadPic/200810/28/2008102819524130s.png)
AJAX在GET中文的时候解决乱码的方法
php递归删除目录与文件的方法
php根据isbn书号查询amazon网站上的图书信息的示例
使用Django和Postgres进行全文搜索的实例代码
经典的退出浏览器弹窗代码
CSS3属性box-shadow图层阴影效果使用教程
![](https://img.aspxhome.com/file/UploadPic/20105/16/01-89s.jpg)
本地机apache配置基于域名的虚拟主机详解
Python爬虫之网络请求
![](https://img.aspxhome.com/file/2023/2/76072_0s.png)
python常见的占位符总结及用法
使用Python脚本将文字转换为图片的实例分享
![](https://img.aspxhome.com/file/2023/5/72935_0s.png)
多个版本的python共存时使用pip的正确做法
python简单程序读取串口信息的方法
python高效过滤出文件夹下指定文件名结尾的文件实例
pytorch 如何把图像数据集进行划分成train,test和val
![](https://img.aspxhome.com/file/2023/4/72124_0s.png)