Python 以下代码的数据输出是水平的,是否可以垂直输出
以下代码的数据输出是水平的,可以垂直输出,非常感谢Python 以下代码的数据输出是水平的,是否可以垂直输出,python,beautifulsoup,Python,Beautifulsoup,以下代码的数据输出是水平的,可以垂直输出,非常感谢 from bs4 import BeautifulSoup import urllib2 url1 = "url" content1 = urllib2.urlopen(url1).read() soup = BeautifulSoup(content1,"lxml") div = soup.find("form", {"name": "SelHorse"}) rows = div.findAll('table')[6].findAll(
from bs4 import BeautifulSoup
import urllib2
url1 = "url"
content1 = urllib2.urlopen(url1).read()
soup = BeautifulSoup(content1,"lxml")
div = soup.find("form", {"name": "SelHorse"})
rows = div.findAll('table')[6].findAll('tr')
for row in rows:
print (row.text)
for循环中的每个print语句都将创建一个新行。如果你想把所有的东西都写在一行中(我想这就是你的意思),你必须把所有的东西都收集在一个字符串中。将for循环替换为以下内容:
output = ''
for row in rows:
output += row
print row
您可以找到所有
td
标记,然后使用str.join
Ex:
for row in rows:
if row:
print( " ".join(td.text.strip() for td in row.findAll("td") if td) )
在打印报表中使用此选项:
print ((row.text).strip())
这将删除输入中的新行字符,因此输出之间不会有空行
对于逗号,您可以执行以下操作:
for row in rows:
print ((row.text).strip() + ",")
注意:即使在最后一行也会打印“.”。为了避免这种情况,只需单独打印该行。i、 e.在for循环之外