Skip to content
Advertisement

How do I put a space between every data got from the FOR LOOP

I am a new learner in python and was trying to get some data from 2 webpages using Beautiful Soup and FOR LOOP to loop over it and print that.

from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as Ureq


url = ["https://www.sullinscorp.com/product/?pn=EMC31DRYS-S734&toggle=in","https://www.sullinscorp.com/product/?pn=PBC10SBBN&toggle=in"]

for i in url:
    a = Ureq(i)

    page_html = a.read()
    psoup = soup(page_html, "html.parser")

    c = psoup.find_all("tr")
    for item in c:
        print(item.text)

From the above code you can see that I am able to get output from both the webpages in a sequence but I want to insert a space or some characters between the data of both the webpages to differentiate them. For that I used my little brain and put a print(——) command in the last of my code. But what it does it just printing the character line after every row.

c = psoup.find_all("tr")
    for item in c:
        print(item.text)
        print("------")

Any suggestion on that.

Advertisement

Answer

IIUC, the separator lines should be placed in the first for loop, which iterates over each url and not the text. Therefore you need to remove one indentation level:

for i in url:
    a = Ureq(i)

    page_html = a.read()
    psoup = soup(page_html, "html.parser")

    c = psoup.find_all("tr")
    for item in c:
        print(item.text)
    print("------")
Advertisement