我正在尝试使用以下代码从标签中提取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">
.
- 您想要该列表中的第一个元素。
- 你想要的
innerHtml
从该元素,可以通过decode_contents()
method.
全部一起:
rank = soup.findAll('span',{"class":"tierRank"})[0].decode_contents()
这会将“Master”存储在rank
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)