初学爬虫,希望把爬到的每条信息写入一个字典,然后循环写入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)
writer.writerow