ValueError arrays must all be same length ValueError arrays must all be same length

0 votes
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import pandas as pd
import xlsxwriter

my_url = "https://www.ebay.com/sch/i.html?_from=R40&_trksid=p2495737.m570.l1313.TR11.TRC2.A0.H1.XSamsung+Galaxy+.TRS1&_nkw=Samsung+Galaxy+&_sacat=0"

uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()

page_soup = soup(page_html, "html.parser")

containers = page_soup.findAll("div", {"class": "s-item__info clearfix"})

product = []
price = []
condition = []

# filename = "ebay2021.csv"
# f = open(filename, "w", encoding='utf-8')
# headers = "product_name, product_price, product_condition\n"
# f.write(headers)

for container in containers:
    brand = container.findAll('h3', {'class': 's-item__title'})
    product_name = brand[0].text

    price = container.findAll('span', {'class': 's-item__price'})
    product_price = price[0].text
    #
    condition = container.findAll("span", {"class": "SECONDARY_INFO"})
    product_condition = condition[0].text

    product.append(product_name)
    price.append(product_price)
    condition.append(product_condition)

    df = pd.DataFrame({'Product Name': product, 'Price': price, 'Rating': condition})
    df.to_csv('products.csv', index=False, encoding='utf-8')
Mar 21, 2020 in Python by Edureka
• 140 points

edited Jun 25, 2020 by MD 11,370 views

Hey, You can do this to avoid the error:

a = {'Product Name': product, 'Price': price, 'Rating': condition}
df = pd.DataFrame.from_dict(a, orient='index')
df.transpose()

Yes, this worked! make sure to add the last line otherwise it will not store data into CSV.

Thanks :-)

a = {

    "Price": prices,

    "Property Type" : types,

    "Infromation": descriptions,

    "Location " : addresses,

    "Features" : features

}

df = pd.DataFrame.from_dict(a, orient='index')

df.transpose()

df.to_csv('website3.csv', index=False,header=True, encoding='utf-8')

1 answer to this question.

0 votes

Hi, 

You need to specify index for your DataFrame. Without an index, Pandas will not you to convert your dictionary to DataFrame. You can modify your code as given below.

a = {'Product Name': product, 'Price': price, 'Rating': condition}
df = pd.DataFrame.from_dict(a, orient='index')
df.transpose()
df.to_csv('website3.csv', index=False,header=True, encoding='utf-8')
answered Jun 25, 2020 by MD
• 95,180 points

Related Questions In Python

0 votes
1 answer

ValueError: arrays must all be same length

Hello, For your query you can refer this:https://www.programmersought.com/article/5321489391/ Hope ...READ MORE

answered Sep 21, 2020 in Python by Niroj
• 82,720 points
2,248 views
0 votes
1 answer

In LightFM does the training set need to be the same size as the test set?

I would change the wording. Often in ML, ...READ MORE

answered Sep 21, 2018 in Python by charlie_brown
• 7,780 points
103 views
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,046 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,564 views
0 votes
1 answer

TypeError: list indices must be integers or slices, not str

Hi@akhtar, As we know that the index of ...READ MORE

answered Oct 14, 2020 in Python by MD
• 95,180 points
8,112 views
0 votes
1 answer

ValueError: could not broadcast input array from shape (360,270,3) into shape (360,280,3)

Hi@akhtar, In the above error it shows could not ...READ MORE

answered Apr 9, 2020 in Python by MD
• 95,180 points
2,197 views