簡體   English   中英

如何在從 FOR LOOP 獲得的每個數據之間放置一個空格

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

我是 python 的新學習者,並試圖使用 Beautiful Soup 和 FOR LOOP 從 2 個網頁中獲取一些數據來循環並打印。

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)

從上面的代碼中你可以看到我可以從兩個網頁中按順序獲取 output 但我想在兩個網頁的數據之間插入一個空格或一些字符來區分它們。 為此,我使用了我的小腦袋,在我的代碼的最后一個地方放了一個 print(------) 命令。 但它只是在每一行之后打印字符行。

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

對此有任何建議。

IIUC,分隔線應放置在第一個 for 循環中,該循環遍歷每個 url 而不是文本。 因此,您需要刪除一個縮進級別:

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("------")
#...
c.psoup.find_all("tr")
for item in c:
    print(item.text)
print("-----") #this line executed once the first url treated, before the second

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM