How to save a scraped list in a CSV file?
<p>I wrote this code below, which scrapes from the OED.com website words by subject and date and prints them out in a list.</p>

<pre><code>import requests
import re
import urllib2
import os
import csv

year_search = 1550
subject_search = ['Law']

path = '/Applications/Python 3.5/Economic'
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)

user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
header = {'User-Agent':user_agent}
request = urllib2.Request('http://www.oed.com/', None, header)
f = opener.open(request)
data = f.read()
f.close()
print 'database first access was successful'

resultPath = os.path.join(path, 'OED_table.csv')
htmlPath = os.path.join(path, 'OED.html')
outputw = open(resultPath, 'w')
outputh = open(htmlPath, 'w')
request = urllib2.Request(
    'http://www.oed.com/search?browseType=sortAlpha&amp;case-insensitive=true'
    '&amp;dateFilter='+str(year_search)+'&amp;nearDistance=1&amp;ordered=false&amp;page=1'
    '&amp;pageSize=100&amp;scope=ENTRY&amp;sort=entry&amp;subjectClass='
    + str(subject_search) + '&amp;type=dictionarysearch', None, header)
page = opener.open(request)
urlpage = page.read()
outputh.write(urlpage)
new_word = re.findall(
    r'&lt;span class=\"hwSect\"&gt;&lt;span class=\"hw\"&gt;(.*?)&lt;/span&gt;', urlpage)
print str(new_word)
outputw.write(str(new_word))
page.close()
outputw.close()
</code></pre>

<p>Now I want to print them to a CSV file but in such a way that every year I input will be placed as a row, and the words will all fall in the line of the row.</p>

<p>Sort of like:</p>

<pre><code>1550| word1| word2| etc.|
1551| word1| word2| etc.|
</code></pre>

<p>Does anyone have any ideas?</p>

<p>After the line where you define <code>new_word</code> you can do the following:</p>

<pre><code>year_info = [str(year_search)] + new_word
print '|'.join(year_info)
</code></pre>

<p>This will output exactly</p>

<p>1550|word1|word2|etc.|</p>

python csv web-scraping