Can't get string inside ATag

117 views Asked by At

I am a beginner, so please be kind. I'm using Beautiful Soup to parse through some html. I have gotten to where i found this a tag

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

I would like to get "S" "hakira" and "Mirfin" out of this string. However when I use the .string function, it just says none. I can get the 'hakira' part, but i can't get the "S" or "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

Any help would be very appreciated!

Thank you.

3

There are 3 answers

0
Jonathan Delean On

Just do that :

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)
  }

if you want to remove all the content :

var children = document.getElementById(id).childNodes;

document.getElementById(id).textContent = ""

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

If it's dont work for your "S" and "Mirfin", you can do that :

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

You can try it:

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)

Output will be:

S,hakira,Mirfin
0
dabingsou On

Another method.

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)

Result:

Shakira Mirfin

Here are more examples: https://github.com/yiyedata/simplified-scrapy-demo/tree/master/doc_examples