Python Forum
[SOLVED] [BeautifulSoup] Turn select() into comma-separated string? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? (/thread-38002.html)



[SOLVED] [BeautifulSoup] Turn select() into comma-separated string? - Winfried - Aug-19-2022

Hello,

I found no example in the tutorial and other sources.

With a select(), I need to find all the elements that match, and turn the results into a comma-separated string, eg. "Author 1, Author 2, Author 3".

I do find the elements, but fail turning them into to a string:
authors_select = soup.select("a[href*=authors]") #OK
authors = ','.join(authors_select)
authors = ','.join(map(str, soup.select("a[href*=authors]"))) 
authors = ','.join(map(str, soup.select("a[href*=authors]").text)) 
print("Authors=",authors)
Thank you.

---
Edit: Found a way

delim = ','
authors = ''
for str in soup.select("a[href*=authors]"):
	authors += (str.text + delim)				
print("Authors=",authors)