0

I am scraping a website and I am stuck. In one print() it is showing 3 lines, but I want to make it one. How can I do it please?

 items = soup.find_all('div', class_='item-cell')
    for item in items:
        low_price = soup.find('div', class_='item-msg is-best-price')
        for string in low_price.strings:
            string = string.string
            print(string)

It prints:

Lowest price
in 
30 days
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 1
    Just use [`join`](https://docs.python.org/3/library/stdtypes.html#str.join): `string = ' '.join(s.string for s in low_price.strings)` – Nick Apr 21 '23 at 00:32

2 Answers2

1

maybe you could concatenate the result

items = soup.find_all('div', class_='item-cell')
    for item in items:
        low_price = soup.find('div', class_='item-msg is-best-price')
        result = ''
        for string in low_price.strings:
            result = result + ' ' + string.string.strip()
        print(result)
0

If one string is printing on three lines, then it probably has newline characters in it. Try this code instead:

 items = soup.find_all('div', class_='item-cell')
    for item in items:
        low_price = soup.find('div', class_='item-msg is-best-price')
        for string in low_price.strings:
            string = string.string
            string = string.replace("\n", "")
            print(string)
Preston Y
  • 121
  • 3