How to print scraped output of 2 p tags in 1 line with a space

Viewed 22

I've added the HTML and the scraper I've written. I want the p (2 p tags) tags content to be printed in 1 line. Like Precious Achiuwa. Then in the next line, I want to print the other 2 p element content.

<td class="primary text RosterRow_primaryCol__19xPQ">
 <a class="flex items-center t6 Anchor_complexLink__2NtkO" href="/player/1630173/precious-achiuwa/">
  <div class="w-8 h-8 mr-2">
   <img alt="Precious Achiuwa Headshot" class="PlayerImage_image__1smob PlayerImage_round__281uY" loading="lazy" src="https://cdn.nba.com/headshots/nba/latest/260x190/1630173.png"/>
  </div>
  <div class="flex flex-col lg:flex-row">
   <p class="t6 mr-1">
    Precious
   </p>
   <p class="t6">
    Achiuwa
   </p>
  </div>
 </a>
</td>
from selenium import webdriver
from bs4 import BeautifulSoup as bs

driver = webdriver.Chrome(
    executable_path=r'C:\Users\silvi\projects\selenium_basics\chromedriver.exe')
url = 'https://www.nba.com/players'
driver.get(url)

soup = bs(driver.page_source, 'lxml')

info_box = soup.find('table', class_='players-list')

names = info_box.find_all('p')
for name in names:
    r = name.get_text(' ', strip=True)
    print(r)

driver.quit()
1 Answers

To print all player names from the page you can use this example:

import requests
from bs4 import BeautifulSoup

url = "https://www.nba.com/players"
soup = BeautifulSoup(requests.get(url).content, "html.parser")

for tag in soup.select("tbody .primary"):
    print(tag.get_text(strip=True, separator=" "))

Prints:

Precious Achiuwa
Jaylen Adams
Steven Adams
Bam Adebayo
LaMarcus Aldridge
Ty-Shon Alexander
Nickeil Alexander-Walker
Grayson Allen
Jarrett Allen
Al-Farouq Aminu
Kyle Anderson
Giannis Antetokounmpo
Kostas Antetokounmpo
Thanasis Antetokounmpo
Carmelo Anthony
Cole Anthony
OG Anunoby
Ryan Arcidiacono
Trevor Ariza
D.J. Augustin
Deni Avdija
Deandre Ayton
Udoka Azubuike
Dwayne Bacon
Marvin Bagley III
LaMelo Ball
Lonzo Ball
Mo Bamba
Desmond Bane
Harrison Barnes
RJ Barrett
Will Barton
Keita Bates-Diop
Nicolas Batum
Aron Baynes
Kent Bazemore
Darius Bazley
Bradley Beal
Malik Beasley
Jordan Bell
DeAndre' Bembry
Davis Bertans
Patrick Beverley
Saddiq Bey
Tyler Bey
Khem Birch
Goga Bitadze
Bismack Biyombo
Nemanja Bjelica
Eric Bledsoe
Related