【问题标题】:Write lists to csv file list index out of range python将列表写入csv文件列表索引超出范围python
【发布时间】:2021-05-04 05:20:04
【问题描述】:

按照 youtube 上的教程,我可以尝试这些行(这些行只工作一次)

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]])

但是当我再次尝试运行它时,我得到了以下错误:

IndexError Traceback(最近一次调用最后一次) 在 31 for i in range(len(Names)): 32 #print(i,名称[i],价格[i]) ---> 33 writer.writerow([i, Names[i], Price[i]])

IndexError: 列表索引超出范围

【问题讨论】:

  • Prices 的数量很可能少于 Names。在写入 csv 文件之前尝试打印出 len(Names)len(Prices)

标签: python beautifulsoup python-requests


【解决方案1】:

您的NamesPrices 的长度不同。

你可能想试试itertools.zip_longest()

方法如下:

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)

输出:

【讨论】:

    【解决方案2】:

    您的结果可能在长度上有所不同。

    只运行你的循环直到更小的循环。

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

    【讨论】:

    • for i, (name, price) in enumerate(zip(Names, Prices)):
    • 不错!不知道 zip 照顾长度!
    猜你喜欢
    • 2017-05-11
    • 1970-01-01
    • 2016-05-29
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多