如何在Python中使用BeautifulSoup从标签中提取innerHTML

2023-12-26

我正在尝试使用以下代码从标签中提取innerHTML:

theurl = "http://na.op.gg/summoner/userName=Darshan"
thepage = urlopen(theurl)
soup = BeautifulSoup(thepage,"html.parser")
rank = soup.findAll('span',{"class":"tierRank"})

不过我得到了[< span class="tierRank" > Master < /span >]反而。 我想展示的只是“Master”这个值。

Using soup.get_text代替soup.findall不起作用。

我尝试添加.text and .string到最后一行的末尾,但这也不起作用。


soup.findAll('span',{"class":"tierRank"})返回一个list匹配的元素数<span class="tierRank">.

  1. 您想要该列表中的第一个元素。
  2. 你想要的innerHtml从该元素,可以通过decode_contents() method.

全部一起:

rank = soup.findAll('span',{"class":"tierRank"})[0].decode_contents()

这会将“Master”存储在rank.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在Python中使用BeautifulSoup从标签中提取innerHTML 的相关文章

随机推荐