JohnR4785
JohnR4785

Reputation: 73

Python - Printing on Same Line

I am very new and attempting to learn how to scrape tables. I have the following code, but can not get the two variables to print on the same line; they print on separate lines. What am I missing?

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

Upvotes: 4

Views: 475

Answers (2)

persian_dev
persian_dev

Reputation: 83

I think you should use this :

Second_row_first_column = table.findAll('tr')[1].findAll('td')[0].text.rstrip('\n') + "\t"

Upvotes: 0

L3viathan
L3viathan

Reputation: 27283

The columns have a newline at the end, so if you want to print it without them, you have to .strip() them:

print Second_row_first_column.strip() + Second_row_second_column.strip()

If you want a space between the two columns, replace the plus with a comma.

Upvotes: 4

Related Questions