0

Following a tutorial on youtube I could try those lines (Those lines worked for once)

from bs4 import BeautifulSoup
import requests
import csv

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}

source = requests.get('https://www.amazon.in/s?k=Laptops&ref=nb_sb_noss_2', headers = headers).text
soup = BeautifulSoup(source, 'lxml')

# print(soup.prettify())

Names = []
Prices = []

# for loop

for i in soup.find_all('a', class_='a-link-normal a-text-normal'):
    string = i.text
    Names.append( string.strip() )

for i in soup.find_all('span', class_='a-price-whole'):
    Prices.append(i.text)


file_name = 'Laptops.csv'

with open(file_name, 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['Sr.No', 'Laptop Name', 'Prices'])

    for i in range(len(Names)):
        #print(i, Names[i], Prices[i])
        writer.writerow([i, Names[i], Prices[i]])

But when trying to run it again I got the following error:

IndexError Traceback (most recent call last) in 31 for i in range(len(Names)): 32 #print(i, Names[i], Prices[i]) ---> 33 writer.writerow([i, Names[i], Prices[i]])

IndexError: list index out of range

1
  • 1
    There are most likely less Prices than Names. Try printing out len(Names) and len(Prices) before writing the csv file. Commented Jan 30, 2021 at 14:08

2 Answers 2

1

Your Names are of different length than Prices.

You might want to try itertools.zip_longest().

Here's how:

import itertools

from bs4 import BeautifulSoup
import requests
import csv

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                  'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
}

url = 'https://www.amazon.in/s?k=Laptops&ref=nb_sb_noss_2'
source = requests.get(url, headers=headers)
soup = BeautifulSoup(source.text, 'lxml')

Names = [
    i.getText(strip=True) for i in
    soup.find_all('a', class_='a-link-normal a-text-normal')
]
Prices = [
    i.getText(strip=True) for i in
    soup.find_all('span', class_='a-price-whole')
]

with open("Laptops.csv", "w") as f:
    w = csv.writer(f)
    data = list(
        itertools.zip_longest(
            list(range(1, len(Names) + 1)),
            Names,
            Prices,
            fillvalue="N/A",
        )
    )
    w.writerows(data)

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

Your result might be differing in lenghts.

Run your loop only till smaller one.

for i in range(min(len(Prices),len(Names))):

2 Comments

for i, (name, price) in enumerate(zip(Names, Prices)):
Nice! Did not know zip takes care of lengths!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.