求教python3.5如何把字典循环写入csv?

初学爬虫,希望把爬到的每条信息写入一个字典,然后循环写入csv文件。但是总是报错显示ValueError: dict contains fields not in fieldnames: 'p',求指教如何修改可以把每个字典作为一行循环写入?

代码如下:

import requests
from bs4 import BeautifulSoup
import csv

def GetInfo(url):
    res=requests.get(url).text
    soup=BeautifulSoup(res,'html.parser')
    names=soup.select('div.list > ul > li > div > p.infoBox > a')
    years=soup.select('div.list > ul > li > div > p.fc-gray > span:nth-of-type(2)')
    prices0=soup.select('body > div.w > div.list > ul > li > div > p.priType-s > s')
    prices1=soup.select('div.list > ul > li > div > p.priType-s > span > i')
    for name,year,price0,price1 in zip(names,years,prices0,prices1):
        data={
            'name':name.get_text(),
            'year':year.get_text(),
            'price0':price0.get_text(),
            'price1':price1.get_text().strip()
        }
    return(data)

def Pages():
    pageurl='https://www.guazi.com/www/buy/o{}/'
    urls=[pageurl.format(str(i)) for i in range(1,2,1)]
    for url in urls:
        a=GetInfo(url)
    return a

with open('car-test.csv','w',newline='') as csvfile:
    headers=['name','year','price0','price1']
    rows=Pages()
    writer=csv.DictWriter(csvfile,headers)
    writer.writeheader()
    for info in rows:
        writer.writerows(info)
阅读 7.8k
1 个回答

writer.writerow

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏