python for循环变量并保存到csv

时间:2016-02-16 18:14:22

标签: python csv zip

这是一个由两部分组成的问题:

我已经测试了我的代码并且它正在运行,但最后一篇文章我无法弄清楚。 我已经能够打印出来并且它可以正常工作,但是当我尝试将其保存到文件时,我遇到了错误。在我运行for循环并打印后,输出看起来像这样(这可能无法在此站点上正确显示,但实际上它是垂直的):

42322

Signal Lighting
Brackets & Grommets
Vertical Mount MicroNova® Dot LED License  [...]
42332

Signal Lighting
Brackets & Grommets
Vertical Mount MicroNova® Dot LED License  [...]
43022

Signal Lighting
Brackets & Grommets
Universal Steel Offset License Plate Brack [...]
43022-3

Signal Lighting
Brackets & Grommets
Universal Steel Offset License Plate Brack [...]

这是我希望它在csv文件中的样子(将垂直水平转动):

42322   Signal Lighting Brackets & Grommets Vertical Mount MicroNova® Dot LED License  [...]
42332   Signal Lighting Brackets & Grommets Vertical Mount MicroNova® Dot LED License  [...]
43022   Signal Lighting Brackets & Grommets Universal Steel Offset License Plate Brack [...]
43022-3 Signal Lighting Brackets & Grommets Universal Steel Offset License Plate Brack [...]

这是我打印的代码:

import urllib
import urllib.request
from bs4 import BeautifulSoup
import os
import csv

f = open('gropn1.csv')
csv_f = csv.reader(f)

for row in csv_f:

    theurl="http://www.grote.com/?s="+row[1] + "&q1=1"
    thepage = urllib.request.urlopen(theurl)
    soup = BeautifulSoup(thepage,"html.parser")

    for partno in soup.find_all('h2',{"class":"single-product-number"}):
        print(partno.text)

    for link in soup.find('ul',{"class":"breadcrumbs"}).findAll('a'):
        print(link.text)
f.close()

要将它保存到文件中,我尝试过这一点 - 从第一个更改为partno循环:

    partno = soup.find_all('h2',{"class":"single-product-number"})
    link = soup.find('ul',{"class":"breadcrumbs"}).findAll('a')

file = open(os.path.expanduser("grotes.csv"),"wb")
file.write(bytes(partno, encoding="ascii",errors='ignore'))
file.write(bytes(link, encoding="ascii",errors='ignore'))

f.close

我尝试了其他的东西,但我认为上面的内容与我的工作一样接近,不包括尝试使用zip功能 - 这是我得到的错误:

Traceback (most recent call last):
  File "C:/Users/PPluck/Downloads/grotestart3.py", line 24, in <module>
    file.write(bytes(partno, encoding="ascii",errors='ignore'))
TypeError: encoding or errors without a string argument

抱歉这个疯狂冗长的问题。

0 个答案:

没有答案