I have a link, and within that link, I have some products. Within each of these products, there is a table of specifications. The table is such that first column should be the header, and second column the data corresponding to it. The first column for each of these tables is different, with some overlapping categories. I want to get one big table that has all these categories, and in rows, the different products. I am able to get data for one table (one product) as follows:
import requests
import csv
from bs4 import BeautifulSoup
def cpap_spider(max_pages):
page=1
while page<=max_pages:
url= "https://www.1800cpap.com/cpap-masks/nasal?page=" +str(page)
source_code= requests.get(url)
plain_text= source_code.text
soup= BeautifulSoup(plain_text, 'html.parser')
for link in soup.findAll("a", {"class":"facets-item-cell-grid-title"}):
href="https://www.1800cpap.com"+link.get("href")
title= link.string
each_item(href)
print(href)
#print(title)
page+=1
data=[]
def each_item(item_url):
source_code= requests.get(item_url)
plain_text= source_code.text
soup= BeautifulSoup(plain_text, 'html.parser')
table=soup.find("table", {"class":"table"})
table_rows= table.find_all('tr')
for row in table_rows:
cols = row.find_all('td')
cols = [ele.text.strip() for ele in cols]
data.append([ele for ele in cols if ele]) # Get rid of empty values
b = open('all_appended.csv', 'w')
a = csv.writer(b)
a.writerows(data)
b.close()
cpap_spider(1)
This code gets all the tables appended , one after the other. However, I wanted a single big table with unique headers in the first row, and corresponding values of products in the sequential order.