【问题标题】:Looping over a csv, trying to write to another csv循环一个 csv,试图写入另一个 csv
【发布时间】:2018-05-23 14:25:34
【问题描述】:

我正在遍历链接的 csv,访问这些链接,然后尝试将这些链接中的信息写入新文件:

with open("hrefs.csv", "rb") as f:
    reader = csv.reader(f)

    for row in reader:
        newUrl = row[0]

        response = requests.get(newUrl)
        newData = response.text
        newSoup = BeautifulSoup(newData, 'lxml')
        newstring = ''

        titles = newSoup.findAll('span', {'id': 'titletextonly'})
        prices = newSoup.findAll('span', {'class': 'price'})
        newstring += titles[0].text + ',' + prices[0].text + ','
        for ana in newSoup.findAll('p',{'class':'attrgroup'}):
            for myb in ana.findAll('b'):
                newstring += myb.text + ','
        print newstring
        listFile = open("output.csv", 'wb')
        writer = csv.writer(listFile)
        writer.writerow(newstring.encode('ascii', 'ignore').decode('ascii'))

我遇到了几个问题。 首先,我认为 csv 会意识到存在逗号分隔值并将每个属性放在一个新列中。 第二,似乎每列都有一个字母。当我简单的print 每个newstring 它给了我一个连贯的字符串。

【问题讨论】:

  • writer.writerow 期望列表/元组代表一行。相反,你给它一个字符串,它是一个序列,所以它假设每个单独的字母在它遍历字符串时都在它自己的列中。不过,我对 BeatutifulSoup 并没有真正的经验,所以我无法从您收到的回复中想象出如何正确使用它。
  • 在 CSV 中写入单个“单元格”的字符串中包含逗号是完全可以接受的。 csv 模块无法知道您是否真的要在遇到的每个逗号上进行拆分;它依赖于你告诉它如何通过提供一个列表来分离数据。

标签: python csv beautifulsoup


【解决方案1】:

你需要给writer.writerow一个字符串序列:

writer.writerow(newstring.split(","))

将是您目前拥有的最简单的更改。

【讨论】:

  • 我写了writer.writerow(newstring.encode('ascii', 'ignore').decode('ascii').split(",")),它写了前几行,但随后中断,消息IndexError: list index out of range
  • 来自 csv 的第 45 行:File "example.py", line 45, in <module> newstring += titles[0].text + ',' + prices[0].text + ',' IndexError: list index out of range
  • 所以这是传入数据的问题。你用 BeautifulSoup 解析它,并期望得到标题和价格。 IndexError 基本上是说前两行中的一条或两条都没有得到任何东西。 titlesprices 将是空列表。
  • 哦,我明白了 - 有没有办法处理异常?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-15
  • 2019-10-05
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 2014-12-11
相关资源
最近更新 更多