无法在 ATag 中获取字符串



我是初学者,所以请善待。我正在使用美丽的汤来解析一些html。我已经到了我找到这个标签的地方

a_tag = <a href="sicc2020/results?pid=31022">S<span class="notCompact">hakira</span> Mirfin</a>

我想从这个字符串中取出"S"hakira"和"Mirfin"。但是,当我使用 .string 函数时,它只是说没有。我可以得到"hakira"部分,但我不能得到"S"或"Mirfin"。

print(a_tag)
>><a href="sicc2020/results?pid=31022">S<span class="notCompact">hakira</span> Mirfin</a>
print(a_tag).string
>> None
print(a_tag).find('span').string
>>hakira

任何帮助将不胜感激!

谢谢。

你可以试试:

from bs4 import BeautifulSoup
html_doc="""<a href="sicc2020/results?pid=31022">S<span class="notCompact">hakira</span> Mirfin</a>"""
soup = BeautifulSoup(html_doc, 'lxml')
text = soup.find("a").get_text(",", strip=True)
print(text)

输出将是:

S,hakira,Mirfin

只需这样做:

var text_array;
var children = document.getElementById(id).childNodes;
text_array.push(document.getElementById(id).textContent)
for (var i = 0; i < children.length; i++) {
text_array.push(children[i].textContent)
}

如果要删除所有内容:

var children = document.getElementById(id).childNodes;
document.getElementById(id).textContent = ""
for (var i = 0; i < children.length; i++) {
children[i].textContent = ""
}

如果它不适用于您的"S"和"Mirfin",您可以这样做:

$("#id")
.clone()    //clone the element
.children() //select all the children
.remove()   //remove all the children
.end()  //again go back to selected element
.text();

另一种方法。

from simplified_scrapy import SimplifiedDoc,req,utils
html ='''<a href="sicc2020/results?pid=31022">S<span class="notCompact">hakira</span> Mirfin</a>'''
doc = SimplifiedDoc(html)
print (doc.a.text)

结果:

Shakira Mirfin

以下是更多示例: https://github.com/yiyedata/simplified-scrapy-demo/tree/master/doc_examples

最新更新