【问题标题】:writing info to csv in python在python中将信息写入csv
【发布时间】:2015-08-08 20:19:11
【问题描述】:
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: 需要一个缩进块

【问题讨论】:

  • 具体是什么错误?
  • IndentationError: 需要一个缩进块
  • 您很可能遇到了空格错误。检查所有空格是否等于制表位(不推荐)或每个缩进级别是否完全匹配四个空格(推荐)
  • 另外,如果继续出现缩进错误,请指出行号是什么。

标签: python csv web-scraping


【解决方案1】:

我认为首先要考虑的是移动打开文件并在循环之外创建 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)

【讨论】:

    猜你喜欢
    • 2018-03-23
    • 1970-01-01
    • 2013-10-04
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    相关资源
    最近更新 更多