html页面如下
<tr style="background-color:#fff;">
<td colspan="2" align=left valign="top">
<table id="zoom2" width="94%" border="0" cellspacing="0" cellpadding="0" style="margin:0 auto">
<!--startprint-->
<tr>
<th scope="col" id="DetailTilte">
<h1>微博客信息服务管理规定</h1>
</th>
</tr>
<tr>
<td scope="col" id="DetailContent">
用法:
def get_title(url):
resp = urllib.request.urlopen(url)
html = resp.read()
bs = BeautifulSoup(html, "html.parser")
title = bs.find('th', id='DetailTilte').h1.get_text()
return title
bs.find第一个参数表示标签的名字,可以是'a' ,'p'之类,代码寻找的是a标签或者是p标签
后面跟一个属性,可以是id,可以是name,或者其他的一些属性,我这里填写了id='DetailTitle'
完了之后会得到
<th scope="col" id="DetailTilte">
<h1>微博客信息服务管理规定</h1>
</th>
这样一个字符串,我们需要得到这个th标签里面的h1,所以把h1给提出来,且获取他的文案
h1.get_text()
原文链接:https://blog.csdn.net/lynn_coder/article/details/79509863
本文介绍了一个使用Python的BeautifulSoup库从特定HTML结构中提取标题的方法。通过示例展示了如何定位并获取指定ID的th元素内的h1标签文本。
&spm=1001.2101.3001.5002&articleId=101355148&d=1&t=3&u=e33566f9b1704b549b573eec26c0dd06)
3020

被折叠的 条评论
为什么被折叠?



