html页面如下

<tr style="background-color:#fff;">
<td colspan="2" align=left valign="top">
<table >

用法:

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

相关文章:

  • 2022-02-23
  • 2021-10-29
  • 2022-12-23
  • 2022-12-23
  • 2021-10-01
  • 2022-12-23
猜你喜欢
  • 2021-12-09
  • 2021-10-17
  • 2022-12-23
  • 2022-12-23
  • 2022-01-02
  • 2022-12-23
  • 2021-07-13
相关资源
相似解决方案