Home > database >  Retrieving a tag parameter with a dash bs4 python
Retrieving a tag parameter with a dash bs4 python

Time:01-10

I need to get a tag that has a dash("-") in its arguments. Python thinks I've entered the wrong syntax in ** kwargs and am trying to subtract something. I've tried writing the tag name in quotes or as a separate variable as a string, but it doesn't work.

HTML:

<vim-dnd ta-id="5ec8f69f" sync-id="m9040768DC9">i need to get this tag</vim-dnd>

Python:

get_id = "5ec8f69f"
get_tag_by_id = soup.find_all('vim-dnd', ta-id=get_id)

CodePudding user response:

Try this:

from bs4 import BeautifulSoup

sample = """<vim-dnd ta-id="5ec8f69f" sync-id="m9040768DC9">i need to get this tag</vim-dnd>"""
get_id = "5ec8f69f"
soup = BeautifulSoup(sample, "lxml").find_all("vim-dnd", {"ta-id": get_id})

for item in soup:
    print(item.getText())

Output:

i need to get this tag
  •  Tags:  
  • Related