gpt4 book ai didi

python - 在python中将信息写入csv

转载 作者:太空宇宙 更新时间:2023-11-04 01:05:55 26 4
gpt4 key购买 nike

import requests
from bs4 import BeautifulSoup
import csv
from urlparse import urljoin
import urllib2

base_url = 'http://www.baseball-reference.com/' # base url for concatenation
data = requests.get("http://www.baseball-reference.com/teams/BAL/2014-schedule-scores.shtml") #website for scraping
soup = BeautifulSoup(data.content)
b=5

for link in soup.find_all('a'):

if not link.has_attr('href'):
continue

if link.get_text() != 'boxscore':
continue

url = base_url + link['href']

response = requests.get(url)
html = response.content
soup = BeautifulSoup(html)

# Scores
table = soup.find('table', attrs={'id': 'BaltimoreOriolespitching'})
for row in table.findAll('tr'):
list_of_cells = []
for cell in row.findAll('td'):
text = cell.text.replace(' ', '')
list_of_cells.append(text)
for list in list_of_cells:
with open('test1.csv', 'w', newline='') as fp:
a = csv.writer(fp, delimiter=',')
a.writerows(list)

我正在尝试将抓取的信息写入 csv,以便每条信息都有自己的单元格。我玩代码的次数越多,要么出现缩进错误,要么第一行打印到 csv,仅此而已。

IndentationError: 需要一个缩进 block

最佳答案

我认为首先要考虑的是移动打开文件并在循环外创建 CSV 编写器。我认为您在每次通过 for 循环时都会覆盖 CSV 文件 ('w')。所以试试这个:

with open('test1.csv', 'w', newline='') as fp:
csvw = csv.writer(fp, delimiter=',')

for link in soup.find_all('a'):

if not link.has_attr('href'):
continue

if link.get_text() != 'boxscore':
continue

url = base_url + link['href']

response = requests.get(url)
html = response.content
soup = BeautifulSoup(html)

# Scores
table = soup.find('table', attrs={'id': 'BaltimoreOriolespitching'})
for row in table.findAll('tr'):
list_of_cells = []
for cell in row.findAll('td'):
text = cell.text.replace(' ', '')
list_of_cells.append(text)
for list in list_of_cells:
csvw.writerows(list)

关于python - 在python中将信息写入csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30470734/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com