python网络编程学习笔记(7):HTML和XHTML解析(HTMLParser、BeautifulSoup)

本文涉及的产品
云解析DNS,个人版 1个月
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
简介: 转载请注明:@小五义http://www.cnblogs.com/xiaowuyi 在python中能够进行html和xhtml的库有很多,如HTMLParser、sgmllib、htmllib、BeautifulSoup、mxTidy、uTidylib等,这里介绍一下HTMLParser、BeautifulSoup等模块。

转载请注明:@小五义http://www.cnblogs.com/xiaowuyi

在python中能够进行html和xhtml的库有很多,如HTMLParser、sgmllib、htmllib、BeautifulSoup、mxTidy、uTidylib等,这里介绍一下HTMLParser、BeautifulSoup等模块。

一、利用HTMLParser进行网页解析
具体HTMLParser官方文档可参考http://docs.python.org/library/htmlparser.html#HTMLParser.HTMLParser

1、从一个简单的解析例子开始
例1:
test1.html文件内容如下:

<html> 
<head> 
<title> XHTML 与 HTML 4.01 标准没有太多的不同</title> 
</head> 
<body> 
i love you 
</body> 
</html> 

 

下面是能够列出title和body的程序示例:

##@小五义:http://www.cnblogs.com/xiaowuyi 
##HTMLParser示例 
import HTMLParser 
class TitleParser(HTMLParser.HTMLParser): 
    def __init__(self): 
        self.taglevels=[] 
        self.handledtags=['title','body'] #提出标签 
        self.processing=None 
        HTMLParser.HTMLParser.__init__(self) 
    def handle_starttag(self,tag,attrs): 
        if tag in self.handledtags: 
            self.data='' 
            self.processing=tag 
    def handle_data(self,data): 
        if self.processing: 
            self.data +=data 
    def handle_endtag(self,tag): 
        if tag==self.processing: 
            print str(tag)+':'+str(tp.gettitle()) 
            self.processing=None 
    def gettitle(self): 
        return self.data 
fd=open('test1.html') 
tp=TitleParser() 
tp.feed(fd.read()) 

 

运行结果如下:
title: XHTML 与 HTML 4.01 标准没有太多的不同
body:
i love you
程序定义了一个TitleParser类,它是HTMLParser类的子孙。HTMLParser的feed方法将接收数据,并通过定义的HTMLParser对象对数据进行相应的解析。其中handle_starttag、handle_endtag判断起始和终止tag,handle_data检查是否取得数据,如果self.processing不为None,那么就取得数据。

2、解决html实体问题
(HTML 中有用的字符实体)
(1)实体名称
当与到HTML中的实体问题时,上面的例子就无法实现,如这里将test1.html的代码改为:
例2:

<html> 
<head> 
<title> XHTML 与&quot; HTML 4.01 &quot;标准没有太多的不同</title> 
</head> 
<body> 
i love you&times; 
</body> 
</html> 

 

利用上面的例子进行分析,其结果是:
title: XHTML 与 HTML 4.01 标准没有太多的不同
body:
i love you
实体完全消失了。这是因为当出现实体的时候,HTMLParser调用了handle_entityref()方法,因为代码中没有定义这个方法,所以就什么都没有做。经过修改后,如下:

##@小五义:http://www.cnblogs.com/xiaowuyi 
##HTMLParser示例:解决实体问题 
from htmlentitydefs import entitydefs 
import HTMLParser 
class TitleParser(HTMLParser.HTMLParser): 
    def __init__(self): 
        self.taglevels=[] 
        self.handledtags=['title','body'] 
        self.processing=None 
        HTMLParser.HTMLParser.__init__(self) 
    def handle_starttag(self,tag,attrs): 
        if tag in self.handledtags: 
            self.data='' 
            self.processing=tag 
    def handle_data(self,data): 
        if self.processing: 
            self.data +=data 
    def handle_endtag(self,tag): 
        if tag==self.processing: 
            print str(tag)+':'+str(tp.gettitle()) 
            self.processing=None 
    def handle_entityref(self,name): 
        if entitydefs.has_key(name): 
            self.handle_data(entitydefs[name]) 
        else: 
            self.handle_data('&'+name+';') 
    def gettitle(self): 
        return self.data 
fd=open('test1.html') 
tp=TitleParser() 
tp.feed(fd.read()) 

 

运行结果为:
title: XHTML 与" HTML 4.01 "标准没有太多的不同
body:
i love you×
这里就把所有的实体显示出来了。

(2)实体编码
例3:

<html> 
<head> 
<title> XHTML 与&quot; HTML 4.01 &quot;标准没有太多的不同</title> 
</head> 
<body> 
i love&#247; you&times; 
</body> 
</html> 

 

如果利用例2的代码执行后结果为:
title: XHTML 与" HTML 4.01 "标准没有太多的不同
body:
i love you×
结果中&#247; 对应的÷没有显示出来。
添加handle_charref()进行处理,具体代码如下:

##@小五义:http://www.cnblogs.com/xiaowuyi 
##HTMLParser示例:解决实体问题 
from htmlentitydefs import entitydefs 
import HTMLParser 
class TitleParser(HTMLParser.HTMLParser): 
    def __init__(self): 
        self.taglevels=[] 
        self.handledtags=['title','body'] 
        self.processing=None 
        HTMLParser.HTMLParser.__init__(self) 
    def handle_starttag(self,tag,attrs): 
        if tag in self.handledtags: 
            self.data='' 
            self.processing=tag 
    def handle_data(self,data): 
        if self.processing: 
            self.data +=data 
    def handle_endtag(self,tag): 
        if tag==self.processing: 
            print str(tag)+':'+str(tp.gettitle()) 
            self.processing=None 
    def handle_entityref(self,name): 
        if entitydefs.has_key(name): 
            self.handle_data(entitydefs[name]) 
        else: 
            self.handle_data('&'+name+';') 

    def handle_charref(self,name): 
        try: 
            charnum=int(name) 
        except ValueError: 
            return 
        if charnum<1 or charnum>255: 
            return 
        self.handle_data(chr(charnum)) 

    def gettitle(self): 
        return self.data 
fd=open('test1.html') 
tp=TitleParser() 
tp.feed(fd.read()) 

 

运行结果为:
title: XHTML 与" HTML 4.01 "标准没有太多的不同
body:
i love÷ you×

3、提取链接
例4: 

<html> 
<head> 
<title> XHTML 与&quot; HTML 4.01 &quot;标准没有太多的不同</title> 
</head> 
<body> 

<a href="http://pypi.python.org/pypi" title="link1">i love&#247; you&times;</a> 
</body> 
</html>

 


这里在handle_starttag(self,tag,attrs)中,tag=a时,attrs记录了属性值,因此只需要将attrs中name=href的value提出即可。具体如下:

##@小五义:http://www.cnblogs.com/xiaowuyi 
##HTMLParser示例:提取链接 
# -*- coding: cp936 -*- 
from htmlentitydefs import entitydefs 
import HTMLParser 
class TitleParser(HTMLParser.HTMLParser): 
    def __init__(self): 
        self.taglevels=[] 
        self.handledtags=['title','body'] 
        self.processing=None 
        HTMLParser.HTMLParser.__init__(self)        
    def handle_starttag(self,tag,attrs): 
        if tag in self.handledtags: 
            self.data='' 
            self.processing=tag 
        if tag =='a': 
            for name,value in attrs: 
                if name=='href': 
                    print '连接地址:'+value 
    def handle_data(self,data): 
        if self.processing: 
            self.data +=data 
    def handle_endtag(self,tag): 
        if tag==self.processing: 
            print str(tag)+':'+str(tp.gettitle()) 
            self.processing=None 
    def handle_entityref(self,name): 
        if entitydefs.has_key(name): 
            self.handle_data(entitydefs[name]) 
        else: 
            self.handle_data('&'+name+';') 

    def handle_charref(self,name): 
        try: 
            charnum=int(name) 
        except ValueError: 
            return 
        if charnum<1 or charnum>255: 
            return 
        self.handle_data(chr(charnum)) 

    def gettitle(self): 
        return self.data 
fd=open('test1.html') 
tp=TitleParser() 
tp.feed(fd.read()) 

 

运行结果为:
title: XHTML 与" HTML 4.01 "标准没有太多的不同
连接地址:http://pypi.python.org/pypi
body:

i love÷ you×

4、提取图片
如果网页中有一个图片文件,将其提取出来,并存为一个单独的文件。
例5:

<html> 
<head> 
<title> XHTML 与&quot; HTML 4.01 &quot;标准没有太多的不同</title> 
</head> 
<body> 
i love&#247; you&times; 
<a href="http://pypi.python.org/pypi" title="link1">我想你</a> 
<div id="m"><img src="http://www.baidu.com/img/baidu_sylogo1.gif" width="270" height="129" ></div> 
</body> 
</html> 

 


将baidu_sylogo1.gif存取出来,具体代码如下:

##@小五义:http://www.cnblogs.com/xiaowuyi 
##HTMLParser示例:提取图片 
# -*- coding: cp936 -*- 
from htmlentitydefs import entitydefs 
import HTMLParser,urllib 
def getimage(addr):#提取图片并存在当前目录下 
    u = urllib.urlopen(addr) 
    data = u.read() 
    filename=addr.split('/')[-1] 
    f=open(filename,'wb') 
    f.write(data) 
    f.close() 
    print filename+'已经生成!' 

class TitleParser(HTMLParser.HTMLParser): 
    def __init__(self): 
        self.taglevels=[] 
        self.handledtags=['title','body'] 
        self.processing=None 
        HTMLParser.HTMLParser.__init__(self)        
    def handle_starttag(self,tag,attrs): 
        if tag in self.handledtags: 
            self.data='' 
            self.processing=tag 
        if tag =='a': 
            for name,value in attrs: 
                if name=='href': 
                    print '连接地址:'+value 
        if tag=='img': 
            for name,value in attrs: 
                if name=='src': 
                    getimage(value) 
    def handle_data(self,data): 
        if self.processing: 
            self.data +=data 
    def handle_endtag(self,tag): 
        if tag==self.processing: 
            print str(tag)+':'+str(tp.gettitle()) 
            self.processing=None 
    def handle_entityref(self,name): 
        if entitydefs.has_key(name): 
            self.handle_data(entitydefs[name]) 
        else: 
            self.handle_data('&'+name+';') 

    def handle_charref(self,name): 
        try: 
            charnum=int(name) 
        except ValueError: 
            return 
        if charnum<1 or charnum>255: 
            return 
        self.handle_data(chr(charnum)) 

    def gettitle(self): 
        return self.data 
fd=open('test1.html') 
tp=TitleParser() 
tp.feed(fd.read()) 

 

运动结果为:
title: XHTML 与" HTML 4.01 "标准没有太多的不同
连接地址:http://pypi.python.org/pypi
baidu_sylogo1.gif已经生成!
body:
i love÷ you×
?ò????

5、实际例子:
例6、获取人人网首页上的各各链接地址,代码如下:

##@小五义:http://www.cnblogs.com/xiaowuyi 
##HTMLParser示例:获取人人网首页上的各各链接地址 
#coding: utf-8 
from htmlentitydefs import entitydefs 
import HTMLParser,urllib 
def getimage(addr): 
    u = urllib.urlopen(addr) 
    data = u.read() 
    filename=addr.split('/')[-1] 
    f=open(filename,'wb') 
    f.write(data) 
    f.close() 
    print filename+'已经生成!' 
class TitleParser(HTMLParser.HTMLParser): 
    def __init__(self): 
        self.taglevels=[] 
        self.handledtags=['a'] 
        self.processing=None 
        self.linkstring='' 
        self.linkaddr='' 
        HTMLParser.HTMLParser.__init__(self)        
    def handle_starttag(self,tag,attrs): 
        if tag in self.handledtags: 
            for name,value in attrs: 
                if name=='href': 
                    self.linkaddr=value 
            self.processing=tag 

    def handle_data(self,data): 
        if self.processing: 
            self.linkstring +=data 
            #print data.decode('utf-8')+':'+self.linkaddr 
    def handle_endtag(self,tag): 
        if tag==self.processing: 
            print self.linkstring.decode('utf-8')+':'+self.linkaddr 
            self.processing=None 
            self.linkstring='' 
    def handle_entityref(self,name): 
        if entitydefs.has_key(name): 
            self.handle_data(entitydefs[name]) 
        else: 
            self.handle_data('&'+name+';') 

    def handle_charref(self,name): 
        try: 
            charnum=int(name) 
        except ValueError: 
            return 
        if charnum<1 or charnum>255: 
            return 
        self.handle_data(chr(charnum)) 

    def gettitle(self): 
        return self.linkaddr 
tp=TitleParser() 
tp.feed(urllib.urlopen('http://www.renren.com/').read())

 

运行结果:
分享:http://***
应用程序:http://app.renren.com
公共主页:http://page.renren.com
人人生活:http://life.renren.com
人人小组:http://xiaozu.renren.com/
同名同姓:http://name.renren.com
人人中学:http://school.renren.com/allpages.html
大学百科:http://school.renren.com/daxue/
人人热点:http://life.renren.com/hot
人人小站:http://zhan.renren.com/
人人逛街:http://j.renren.com/
人人校招:http://xiaozhao.renren.com/
:http://www.renren.com
注册:http://wwv.renren.com/xn.do?ss=10113&rt=27
登录:http://www.renren.com/
帮助:http://support.renren.com/helpcenter
给我们提建议:http://support.renren.com/link/suggest
更多:#
:javascript:closeError();
打开邮箱查收确认信:#
重新输入:javascript:closeError();
:javascript:closeStop();
客服:http://help.renren.com/#http://help.renren.com/support/contomvice?pid=2&selection={couId:193,proId:342,cityId:1000375}
:javascript:closeLock();
立即解锁:http://safe.renren.com/relive.do
忘记密码?:http://safe.renren.com/findPass.do
忘记密码?:http://safe.renren.com/findPass.do
换一张:javascript:refreshCode_login();
MSN:#
360:https://openapi.360.cn/oauth2/authorize?client_id=5ddda4458747126a583c5d58716bab4c&response_type=code&redirect_uri=http://www.renren.com/bind/tsz/tszLoginCallBack&scope=basic&display=default
天翼:https://oauth.api.189.cn/emp/oauth2/authorize?app_id=296961050000000294&response_type=code&redirect_uri=http://www.renren.com/bind/ty/tyLoginCallBack
为什么要填写我的生日?:#birthday
看不清换一张?:javascript:refreshCode();
想了解更多人人网功能?点击此处:javascript:;
:javascript:;
:javascript:;
立刻注册:http://reg.renren.com/xn6245.do?ss=10113&rt=27
关于:http://www.renren.com/siteinfo/about
开放平台:http://dev.renren.com
人人游戏:http://wan.renren.com
公共主页:http://page.renren.com/register/regGuide/
手机人人:http://mobile.renren.com/mobilelink.do?psf=40002
团购:http://www.nuomi.com
皆喜网:http://www.jiexi.com
营销服务:http://ads.renren.com
招聘:http://job.renren-inc.com/
客服帮助:http://support.renren.com/helpcenter
隐私:http://www.renren.com/siteinfo/privacy
京ICP证090254号:http://www.miibeian.gov.cn/
互联网药品信息服务资格证:http://a.xnimg.cn/n/core/res/certificate.jpg

二、利用BeautifulSoup进行网页解析
1、BeautifulSoup下载和安装
下载地址:http://www.crummy.com/software/BeautifulSoup/download/3.x/
中文文档地址:http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html#Entity%20Conversion
安装方法:将下载的文件解压缩后,文件夹下有个setup.py文件,然后在cmd下,运行python setup.py install进行安装,注意setup.py的路径问题。安装成功后,在python中就可以直接import BeautifulSoup了。
2、从一个简单的解析例子开始
例7:

<html> 
<head> 
<title> XHTML 与&quot; HTML 4.01 &quot;标准没有太多的不同</title> 
</head> 
<body> 
i love&#247; you&times; 
<a href="http://pypi.python.org/pypi" title="link1">我想你</a> 
<div id="m"><img src="http://www.baidu.com/img/baidu_sylogo1.gif" width="270" height="129" ></div> 
</body> 
</html> 

 

获取title的代码:

##@小五义:http://www.cnblogs.com/xiaowuyi 
##BeautifulSoup示例:title 
#coding: utf8 
import BeautifulSoup 

a=open('test1.html','r') 
htmlline=a.read() 
soup=BeautifulSoup.BeautifulSoup(htmlline.decode('gb2312')) 
#print soup.prettify()#规范化html文件 
titleTag=soup.html.head.title 
print titleTag.string 

 

运行结果:
XHTML 与&quot; HTML 4.01 &quot;标准没有太多的不同
从代码和结果来看,应注意两点:
第一,在BeautifulSoup.BeautifulSoup(htmlline.decode('gb2312'))初始化过程中,应注意字符编码格式,从网上搜索了一下,开始用utf-8的编码显示不正常,换为gb2312后显示正常。其实可以用soup.originalEncoding方法来查看原文件的编码格式。
第二,结果中未对字符实体进行处理,在BeautifulSoup中文文档中,有专门对实体转换的解释,这里将上面的代码改为以下代码后,结果将正常显示:

##@小五义:http://www.cnblogs.com/xiaowuyi 
##BeautifulSoup示例:title 
#coding: utf8 
import BeautifulSoup 
a=open('test1.html','r') 
htmlline=a.read() 
soup=BeautifulSoup.BeautifulStoneSoup(htmlline.decode('gb2312'),convertEntities=BeautifulSoup.BeautifulStoneSoup.ALL_ENTITIES) 
#print soup.prettify()#规范化html文件 
titleTag=soup.html.head.title 
print titleTag.string 

 

这里convertEntities=BeautifulSoup.BeautifulStoneSoup.ALL_ENTITIES中的ALL_ENTITIES定义了XML和HTML两者的实体代码。当然,也可以直接用XML_ENTITIES或者HTML_ENTITIES。运行结果如下:
XHTML 与" HTML 4.01 "标准没有太多的不同
3、提取链接
还有用上面的例子,这里代码变为:

##@小五义:http://www.cnblogs.com/xiaowuyi 
##BeautifulSoup示例:提取链接 
#coding: utf8 
import BeautifulSoup 
a=open('test1.html','r') 
htmlline=a.read() 
a.close() 
soup=BeautifulSoup.BeautifulStoneSoup(htmlline.decode('gb2312'),convertEntities=BeautifulSoup.BeautifulStoneSoup.ALL_ENTITIES) 
name=soup.find('a').string 
links=soup.find('a')['href'] 
print name+':'+links 

 

运行结果为:
我想你:http://pypi.python.org/pypi
4、提取图片
依然是用上面的例子,把baidu图片提取出来。
代码为:

##@小五义:http://www.cnblogs.com/xiaowuyi
#coding: utf8 
import BeautifulSoup,urllib 
def getimage(addr):#提取图片并存在当前目录下 
    u = urllib.urlopen(addr) 
    data = u.read() 
    filename=addr.split('/')[-1] 
    f=open(filename,'wb') 
    f.write(data) 
    f.close() 
    print filename+' finished!' 
a=open('test1.html','r') 
htmlline=a.read() 
soup=BeautifulSoup.BeautifulStoneSoup(htmlline.decode('gb2312'),convertEntities=BeautifulSoup.BeautifulStoneSoup.ALL_ENTITIES) 
links=soup.find('img')['src'] 
getimage(links) 

 

提取链接和提取图片两部分主要都是用了find方法,具体方法为:
find(name, attrs, recursive, text, **kwargs)
findAll是列出全部符合条件的,find只列出第一条。这里注意的是findAll返回的是个list。
5、实际例子:
例8、获取人人网首页上的各各链接地址,代码如下:

##@小五义:http://www.cnblogs.com/xiaowuyi 
##BeautifulSoup示例:获取人人网首页上的各各链接地址 
#coding: utf8 
import BeautifulSoup,urllib 
linkname='' 
htmlline=urllib.urlopen('http://www.renren.com/').read() 
soup=BeautifulSoup.BeautifulStoneSoup(htmlline.decode('utf-8')) 
links=soup.findAll('a') 
for i in links: 
    ##判断tag是a的里面,href是否存在。 
    if 'href' in str(i): 
        linkname=i.string 
        linkaddr=i['href'] 
        if 'NoneType' in str(type(linkname)):#当i无内容是linkname为Nonetype类型。 
            print linkaddr 
        else: 
            print linkname+':'+linkaddr 

 

运行结果:
分享:http://***
应用程序:http://app.renren.com
公共主页:http://page.renren.com
人人生活:http://life.renren.com
人人小组:http://xiaozu.renren.com/
同名同姓:http://name.renren.com
人人中学:http://school.renren.com/allpages.html
大学百科:http://school.renren.com/daxue/
人人热点:http://life.renren.com/hot
人人小站:http://zhan.renren.com/
人人逛街:http://j.renren.com/
人人校招:http://xiaozhao.renren.com/
http://www.renren.com
注册:http://wwv.renren.com/xn.do?ss=10113&rt=27
登录:http://www.renren.com/
帮助:http://support.renren.com/helpcenter
给我们提建议:http://support.renren.com/link/suggest
更多:#
javascript:closeError();
打开邮箱查收确认信:#
重新输入:javascript:closeError();
javascript:closeStop();
客服:http://help.renren.com/#http://help.renren.com/support/contomvice?pid=2&selection={couId:193,proId:342,cityId:1000375}
javascript:closeLock();
立即解锁:http://safe.renren.com/relive.do
忘记密码?:http://safe.renren.com/findPass.do
忘记密码?:http://safe.renren.com/findPass.do
换一张:javascript:refreshCode_login();
MSN:#
360:https://openapi.360.cn/oauth2/authorize?client_id=5ddda4458747126a583c5d58716bab4c&response_type=code&redirect_uri=http://www.renren.com/bind/tsz/tszLoginCallBack&scope=basic&display=default
天翼:https://oauth.api.189.cn/emp/oauth2/authorize?app_id=296961050000000294&response_type=code&redirect_uri=http://www.renren.com/bind/ty/tyLoginCallBack
#birthday
看不清换一张?:javascript:refreshCode();
javascript:;
javascript:;
立刻注册:http://reg.renren.com/xn6245.do?ss=10113&rt=27
关于:http://www.renren.com/siteinfo/about
开放平台:http://dev.renren.com
人人游戏:http://wan.renren.com
公共主页:http://page.renren.com/register/regGuide/
手机人人:http://mobile.renren.com/mobilelink.do?psf=40002
团购:http://www.nuomi.com
皆喜网:http://www.jiexi.com
营销服务:http://ads.renren.com
招聘:http://job.renren-inc.com/
客服帮助:http://support.renren.com/helpcenter
隐私:http://www.renren.com/siteinfo/privacy
京ICP证090254号:http://www.miibeian.gov.cn/
互联网药品信息服务资格证:http://a.xnimg.cn/n/core/res/certificate.jpg

目录
相关文章
|
16天前
|
数据采集 存储 API
Python虚拟环境数据共享技术解析:最佳实践与常见误区
本文探讨了Python爬虫开发中如何在虚拟环境中管理数据,提倡使用共享目录、数据库和API进行数据共享。通过创建虚拟环境、安装依赖并提供一个使用代理IP爬取微博数据的示例,阐述了如何配置代理、解析网页及保存数据到共享路径。强调了避免硬编码路径、忽视依赖管理和数据安全性的误区。
37 11
Python虚拟环境数据共享技术解析:最佳实践与常见误区
|
1天前
|
设计模式 算法 关系型数据库
Python面向对象编程基础解析
【7月更文挑战第21天】在Python中,面向对象编程(OOP)是一种强大的编程范式,它允许开发者通过定义类和对象来组织和管理代码。本文将介绍Python中面向对象编程的基础概念,并通过代码实例进行解析。
21 10
|
12天前
|
数据处理 Python
深入探索:Python中的并发编程新纪元——协程与异步函数解析
【7月更文挑战第15天】Python 3.5+引入的协程和异步函数革新了并发编程。协程,轻量级线程,由程序控制切换,降低开销。异步函数是协程的高级形式,允许等待异步操作。通过`asyncio`库,如示例所示,能并发执行任务,提高I/O密集型任务效率,实现并发而非并行,优化CPU利用率。理解和掌握这些工具对于构建高效网络应用至关重要。
24 6
|
15天前
|
算法 Python
Python 大神修炼手册:图的深度优先&广度优先遍历,深入骨髓的解析
【7月更文挑战第12天】Python进阶必学:DFS和BFS图遍历算法。理解图概念,用邻接表建无向图,实现DFS和BFS。DFS适用于查找路径,BFS解决最短路径。通过实例代码加深理解,提升编程技能。
19 4
|
6天前
|
中间件 数据库 开发者
解析Python Web框架的四大支柱:模板、ORM、中间件与路由
【7月更文挑战第20天】Python Web框架如Django、Flask、FastAPI的核心包括模板(如Django的DTL和Flask的Jinja2)、ORM(Django的内置ORM与Flask的SQLAlchemy)、中间件(Django的全局中间件与Flask的装饰器实现)和路由(Django的urls.py配置与Flask的@app.route()装饰器)。这些组件提升了代码组织和数据库操作的便捷性,确保了Web应用的稳定性和可扩展性。
|
10天前
|
运维 负载均衡 前端开发
深度解析:Python Web前后端分离架构中WebSocket的选型与实现策略
【7月更文挑战第16天】Python Web开发中,前后端分离常见于实时通信场景,WebSocket作为全双工协议,常用于此类应用。选型时考虑性能、功能、易用性、社区支持和成本。Flask-SocketIO是实现WebSocket的一个选项,它简化了与Flask的集成。案例展示了如何用Flask-SocketIO创建一个实时聊天室:后端处理消息广播,前端通过Socket.IO库连接并显示消息。此实现策略演示了在Python中实现实时通信的基本步骤。
19 0
|
13天前
|
存储 SQL Python
`urllib.parse`模块是Python标准库`urllib`中的一个子模块,它提供了处理URL(统一资源定位符)的实用功能。这些功能包括解析URL、组合URL、转义URL中的特殊字符等。
`urllib.parse`模块是Python标准库`urllib`中的一个子模块,它提供了处理URL(统一资源定位符)的实用功能。这些功能包括解析URL、组合URL、转义URL中的特殊字符等。
|
13天前
|
存储 Python 容器
`click`是一个用于构建命令行接口的Python包,它提供了简单、可组合的命令行解析器。
`click`是一个用于构建命令行接口的Python包,它提供了简单、可组合的命令行解析器。
|
13天前
|
Unix Linux Shell
Sphinx是一个Python文档生成工具,它可以解析reStructuredText或Markdown格式的源代码注释,并生成多种输出格式,如HTML、LaTeX、PDF、ePub等。
Sphinx是一个Python文档生成工具,它可以解析reStructuredText或Markdown格式的源代码注释,并生成多种输出格式,如HTML、LaTeX、PDF、ePub等。
|
19天前
|
网络协议 安全 Java
Java中的网络编程:Socket编程详解
Java中的网络编程:Socket编程详解

推荐镜像

更多