How can i extract the words which are starting with "icon" from HTML code using python

karthik reddy

I need a python code to extract the selected word using python.

<a class="tel ttel">
<span class="mobilesv icon-hg"></span>
<span class="mobilesv icon-rq"></span>
<span class="mobilesv icon-ba"></span>
<span class="mobilesv icon-rq"></span>
<span class="mobilesv icon-ba"></span>
<span class="mobilesv icon-ikj"></span>
<span class="mobilesv icon-dc"></span>
<span class="mobilesv icon-acb"></span>
<span class="mobilesv icon-lk"></span>
<span class="mobilesv icon-ba"></span>
<span class="mobilesv icon-nm"></span>
<span class="mobilesv icon-ba"></span>
<span class="mobilesv icon-yz"></span>
</a>

I need to extract the words which start with the "icon"

The Output which I required is

icon-hg, icon-rq, icon-ba, icon-rq, icon-ba, icon-ikj, icon-dc, icon-acb, icon-lk, icon-ba, icon-nm, icon-ba, icon-yz

Mahmoud Elshahat

For your specific case you can get it as below, however i recommend using beautiful soup for working with wide problems, remember, Special cases aren't special enough to break the rules.

text = """
<a class="tel ttel">
<span class="mobilesv icon-hg"></span>
<span class="mobilesv icon-rq"></span>
<span class="mobilesv icon-ba"></span>
<span class="mobilesv icon-rq"></span>
<span class="mobilesv icon-ba"></span>
<span class="mobilesv icon-ikj"></span>
<span class="mobilesv icon-dc"></span>
<span class="mobilesv icon-acb"></span>
<span class="mobilesv icon-lk"></span>
<span class="mobilesv icon-ba"></span>
<span class="mobilesv icon-nm"></span>
<span class="mobilesv icon-ba"></span>
<span class="mobilesv icon-yz"></span>
</a>
"""

result = [word.split('"')[0] for word in text.split() if word.startswith('icon')]

print(result)

output:

['icon-hg', 'icon-rq', 'icon-ba', 'icon-rq', 'icon-ba', 'icon-ikj', 'icon-dc', 'icon-acb', 'icon-lk', 'icon-ba', 'icon-nm', 'icon-ba', 'icon-yz']

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I extract information from a HTML code using Python + Selenium?

How can I Extract information from HTML page using Python?

How can i extract words from a string before colon and excluding \n from them in python using regex

How I can extract the portion of words from the file using python3.6?

How to extract words starting with letters from A to L from a text in Python?

How can I extract the nested value from this Javascript/HTML code?

How can I extract a specific text in an html code with Selenium and Python

how can i search within names starting from words in android?

How can i extract href from this html using selenium?

How can I extract URLs from within Javascript code? - Python

How to Extract the Table from the HTML Code using Python BeatifulSoup

How can I extract HTML links from this list in Python?

how can I extract specific row which contain specific keyword from my json dataset using pandas in python?

How can I extract words from a file from a string

How can I extract an exact set of words from a string using a regex?

How can I extract a text from a bytes file using python

How can i extract values from cassandra output using python?

How can I extract links from HTML?

how can I extract data from Json which start from "["?

How to extract exact words from a string using Python and re?

How to extract some words pattern from string using regex in python

How Do I Do 'Words Starting With' in Python?

How to extract all words starting with specific character from string in PostgreSQL?

How can I get all the words starting with {$ of a HTML page with php function

How can I extract images from an MPTS video(which has 4 videos in it) from each and every video using a single command?

How can I write a Python code which will serve a .html file on 0.0.0.0?

How to extract the starting time from a string in Python?

How to extract date from the filename which is in hdf format using Python?

How can I get href links from HTML using Python?