如何在Python中分离Prints?

时间:2016-01-03 19:07:09

标签: python web

所以我有这个代码,我用它来抓取ebay只是为了教育目的而且效果很好。

import urllib.request
from bs4 import BeautifulSoup


url = "http://www.ebay.com/sch/i.html?_from=R40&_trksid=p2050601.m570.l1313.TR12.TRC2.A0.H0.Xps4.TRS0&_nkw=ps4&_sacat=0"

site = urllib.request.urlopen(url)

soup = BeautifulSoup(site.read(), "html.parser")

listings =soup.find_all('a',class_='vip')

for listing in listings:

    print (listing['href']+","+ str(listing.string))

现在,当我打印出来时,我得到this

是否有可能在每个商家信息之间添加空格?无论如何,谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

我假设这是Python 3,因此您可以使用end参数:

# end='\n' won't do anything since '\n' is already the default value for end
print(listing['href']+","+ str(listing.string), end='\n\n')

答案 1 :(得分:0)

我只想做

for listing in listings:
    print(listing['href']+","+ str(listing.string))
    print("\n")

或只是

for listing in listings:
    print(listing['href']+","+ str(listing.string)) + "\n"

“\ n”是换行符。