Convert Response into Comma Separated Text in Python BeautifulSoup

Using BeautifulSoup I am trying to grab some keywords from a website. However I am getting the texts in new lines and don’t have any idea how to turn each response into comma separated text.

My Code:

   with open("tags.txt", "a") as f_out:
        for inp in soup.select('#group-keywords a'):
            keywords = inp.getText()
            readykeywords = keywords.replace('n', ',')
            print(readykeywords, sep="n", file=f_out)

The target HTML looks like this:

<ul style="user-select: auto;" class="selectorgadget_selected">
    <li style="user-select: auto;"><a href="/royalty-free-vectors/fast-vectors" class="selectorgadget_suggested" style="user-select: auto;">Fast</a>
    </li>
    <li style="user-select: auto;"><a href="/royalty-free-vectors/fresh-vectors" class="selectorgadget_suggested" style="user-select: auto;">Fresh</a>
    </li>
    <li style="user-select: auto;"><a href="/royalty-free-vectors/line-vectors" class="selectorgadget_suggested" style="user-select: auto;">Line</a>
    </li>
    <li style="user-select: auto;"><a href="/royalty-free-vectors/paper-vectors" class="selectorgadget_suggested" style="user-select: auto;">Paper</a>
    </li>
    <li style="user-select: auto;"><a href="/royalty-free-vectors/house-vectors" class="selectorgadget_suggested" style="user-select: auto;">House</a>
    </li>
    <li style="user-select: auto;"><a href="/royalty-free-vectors/aroma-vectors" class="selectorgadget_suggested" style="user-select: auto;">Aroma</a>
    </li>
</ul>

OUTPUT:

Fast
Fresh
Line
....

EXPECTED OUTPUT

Fast, Fresh, Line ...

Answer

Try:

from bs4 import BeautifulSoup


html_doc = """
<ul style="user-select: auto;" class="selectorgadget_selected">
    <li style="user-select: auto;"><a href="/royalty-free-vectors/fast-vectors" class="selectorgadget_suggested" style="user-select: auto;">Fast</a>
    </li>
    <li style="user-select: auto;"><a href="/royalty-free-vectors/fresh-vectors" class="selectorgadget_suggested" style="user-select: auto;">Fresh</a>
    </li>
    <li style="user-select: auto;"><a href="/royalty-free-vectors/line-vectors" class="selectorgadget_suggested" style="user-select: auto;">Line</a>
    </li>
    <li style="user-select: auto;"><a href="/royalty-free-vectors/paper-vectors" class="selectorgadget_suggested" style="user-select: auto;">Paper</a>
    </li>
    <li style="user-select: auto;"><a href="/royalty-free-vectors/house-vectors" class="selectorgadget_suggested" style="user-select: auto;">House</a>
    </li>
    <li style="user-select: auto;"><a href="/royalty-free-vectors/aroma-vectors" class="selectorgadget_suggested" style="user-select: auto;">Aroma</a>
    </li>
</ul>"""

soup = BeautifulSoup(html_doc, "html.parser")

with open("tags.txt", "w") as f_out:
    tags = [li.get_text(strip=True) for li in soup.select("a")]
    print(*tags, sep=", ", file=f_out)

Creates tags.txt:

Fast, Fresh, Line, Paper, House, Aroma