python - 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!!
解决方案
推荐阅读
- amazon-web-services - EC2 Image Builder with Amazon Inspector - 不同的规则包?
- android - 选定的弹出菜单项背景颜色集
- php - Ajax 每次都不能正确执行...... 3 次中的 1 次
- dart - 如何为 Future.wait 准备函数列表?
- python - 如何构建特定 python 版本的 rpm 或 tgz,特别是用于 CentOS8 的 python3.7
- okhttp - Okhttp:如何在请求中设置 Host 标头?
- excel - Excel动态下拉菜单
- api - 如何获取谷歌地图静态 API 的图片 URL?
- r - 随机森林的特征选择
- php - 如何将数据打印到 Drupal 中来自内容中另一个数据字段的块?