Python - 在同一行打印
Python - Printing on Same Line
我是新手,正在尝试学习如何抓取表格。我有以下代码,但无法让两个变量在同一行上打印;他们在不同的行上打印。我错过了什么?
from lxml import html
from bs4 import BeautifulSoup
import requests
url = "http://www.columbia.edu/~fdc/sample.html"
r = requests.get(url)
soup = BeautifulSoup(r.content)
tables = soup.findAll('table')
for table in tables:
Second_row_first_column = table.findAll('tr')[1].findAll('td')[0].text
Second_row_second_column = table.findAll('tr')[1].findAll('td')[1].text
print Second_row_first_column + Second_row_second_column
列末尾有一个换行符,所以如果你想在没有它们的情况下打印它,你必须 .strip()
它们:
print Second_row_first_column.strip() + Second_row_second_column.strip()
如果您想要在两列之间使用 space,请将加号替换为逗号。
我认为你应该使用这个:
Second_row_first_column = table.findAll('tr')[1].findAll('td')[0].text.rstrip('\n') + "\t"
我是新手,正在尝试学习如何抓取表格。我有以下代码,但无法让两个变量在同一行上打印;他们在不同的行上打印。我错过了什么?
from lxml import html
from bs4 import BeautifulSoup
import requests
url = "http://www.columbia.edu/~fdc/sample.html"
r = requests.get(url)
soup = BeautifulSoup(r.content)
tables = soup.findAll('table')
for table in tables:
Second_row_first_column = table.findAll('tr')[1].findAll('td')[0].text
Second_row_second_column = table.findAll('tr')[1].findAll('td')[1].text
print Second_row_first_column + Second_row_second_column
列末尾有一个换行符,所以如果你想在没有它们的情况下打印它,你必须 .strip()
它们:
print Second_row_first_column.strip() + Second_row_second_column.strip()
如果您想要在两列之间使用 space,请将加号替换为逗号。
我认为你应该使用这个:
Second_row_first_column = table.findAll('tr')[1].findAll('td')[0].text.rstrip('\n') + "\t"