Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

求教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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

writer.writerow


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...