Reputation: 69
Need help formatting my Python web scrape . For whatever reason when I get the info I need it seems that the words have been tabbed out of place and not sure how to fix it.
Any help is appreciated
Thanks
import requests
from bs4 import BeautifulSoup
r = requests.get("http://www.canadianappliance.ca/Refrigerators-And-Fridges-3/Full-Size-Refrigerators-38/French-Door-Refrigerators-48/?per_page=all")
r.content
soup = BeautifulSoup(r.content)
g_data = soup.find_all("h2", {"class": "product_link"})
for item in g_data:
print (item.text)
Upvotes: 0
Views: 35
Reputation: 473853
Use .get_text()
providing the strip
argument. Additionally, replace newlines with spaces:
g_data = soup.find_all("h2", {"class": "product_link"})
for item in g_data:
print(item.get_text(strip=True).replace("\n", " "))
Prints:
Samsung - RF220NCTASR
Samsung - RF18HFENBSR
Samsung - RF23HCEDBSR
...
Haier - HRF15N3AGS
GE Profile - PWE23KMKES
Upvotes: 1