我正在尝试使用以下代码从标记中提取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”。
使用
soup.get_text
而不是
soup.findall
不起作用。
我尝试将
.text
和
.string
添加到最后一行的末尾,但这也不起作用。
请您参考如下方法:
soup.findAll('span',{"class":"tierRank"})
返回与<span class="tierRank">
匹配的元素列表。
innerHtml
,可以通过decode_contents()
方法进行访问。 全部一起:
rank = soup.findAll('span',{"class":"tierRank"})[0].decode_contents()
这会将“Master”存储在
rank
中。