首页 > 解决方案 > Why does my code only write the first rows into my csv? I'm looping through multiple websites and print function seems to grab all the values

问题描述

I'm trying to scrape for a roster of all past Utah legislators from this website, and I was trying the most basic code to get that. My code can print out all the rows I need, but when I write it into csvs it only documents the first rows. What went wrong?

I thought it might be something wrong with the loop, but the print function seems to give out every row I want on that website. The following is my code:

import urllib2, csv
from bs4 import BeautifulSoup
outfile = open('ut_legislators.csv','w')
writer = csv.writer(outfile)

letters = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

for l in letters:
    url = "https://le.utah.gov/asp/roster/complist.asp?letter=" + l
    html = urllib2.urlopen(url).read()
    soup = BeautifulSoup(html, 'html.parser')

    table = soup.find('table', {'class': 'UItable'})
    rows = table.find_all('tr')

    for row in rows:
        data = []
        cells = row.find_all('td')
        for cell in cells:
            data.append(cell.text.encode('utf-8').strip())
            writer.writerow(data)

And when I open the csv, it only returns the first rows.

Thanks!!

标签: python

解决方案


推荐阅读