My code is
JavaScript
x
25
25
1
from bs4 import BeautifulSoup
2
import pandas as pd
3
4
soup = BeautifulSoup(wd.page_source)
5
items = soup.find("div", {"class": "items-grid-view"})
6
7
rows_processed=[]
8
for item in items.findAll("div", {"class": "item-cell"}):
9
itemTitle = item.find("a", {"class": "item-title"})
10
itemPromo = item.find("p", {"class": "item-promo"})
11
row = []
12
13
row.append(itemTitle.text)
14
if (itemPromo and itemPromo.text == "OUT OF STOCK"):
15
row.append("Sold Out")
16
else:
17
row.append("Available")
18
19
20
rows_processed.append(row)
21
22
df = pd.DataFrame.from_records(rows_processed, columns=["Item Title ", "Status"])
23
24
display(df)
25
When I run the cell it only presents me with the last item in the table when there are supposed to be 12 items shown. It’s not crucial that I use pandas but I’d like to learn so I’m hoping there’s a fix.
Advertisement
Answer
You had the rows_processed.append(row)
outside the for loop, so you were actually just appending the last row. Also pd.DataFrame.from_records
could be just pd.DataFrame
.
JavaScript
1
25
25
1
from bs4 import BeautifulSoup
2
import pandas as pd
3
4
soup = BeautifulSoup(wd.page_source)
5
items = soup.find("div", {"class": "items-grid-view"})
6
7
rows_processed=[]
8
for item in items.findAll("div", {"class": "item-cell"}):
9
itemTitle = item.find("a", {"class": "item-title"})
10
itemPromo = item.find("p", {"class": "item-promo"})
11
row = []
12
13
row.append(itemTitle.text)
14
if itemPromo and itemPromo.text == "OUT OF STOCK":
15
row.append("Sold Out")
16
else:
17
row.append("Available")
18
19
rows_processed.append(row)
20
21
22
df = pd.DataFrame(rows_processed, columns=["Item Title ", "Status"])
23
24
print(df)
25