简体   繁体   中英

writing scraped data to csv file

I am scraping out the 'h2' and 'h3' tags from some html pages and want to write them to a csv file under particular columns. How to create columns and then insert rows under them using python scrapy.

My code is:

def parse(self, response):
    hxs = HtmlXPathSelector(response)
    sites = hxs.select('//ul/li')
    f = open("fquestdata.csv","wb")
    for site in sites:
         quest = site.select('//h2').extract()
         ans = site.select('//h3').extract()
         f.write(ans)

but it gives an error that says:

exceptions.TypeError: must be string or buffer, not list

You are getting more then one answer. Try

for s in ans:
  f.write(s)

Cheers!

why don't you use custom Csv item exporter ? suggested reading

or

write your own code suggested reading

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM