将当前url写入csv TypeError时:只能将列表(不是“元组”)连接到列表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将当前url写入csv TypeError时:只能将列表(不是“元组”)连接到列表相关的知识,希望对你有一定的参考价值。

TypeError:只能连接列表(不是“元组”)以列出将当前url添加到新列

我正在运行一个脚本,它获取当前的URL并将其写入csv文件,但我得到:

    writer.writerow(row + (url1,))
TypeError: can only concatenate list (not "tuple") to list

因为这一行:对于数据中的行:在下面的代码中(不使用driver.current url时有效)

url1 = driver.current_url
with open('test.csv', 'a', newline='', encoding="utf-8") as outfile:
    writer = csv.writer(outfile)
    for row in data:
        writer.writerow(row + (url1,))

如果我添加:

for row in zip(data):

这将所有数据放在一列中,并在第2列中链接,因为我希望每个数据位于不同的列中。

我明白了:

   col1                          col2
['Clyde', 'Annan', '2.65', 'https://www.linkhere.com']

期望:

col1    col2    col3     col4          
Clyde   Annan   2.65    https://www.linkhere.com

知道如何解决这个问题吗?

码:

import csv
import os
import time
from random import shuffle
from selenium import webdriver
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait

driver = webdriver.Chrome()
driver.set_window_size(1024, 600)
driver.maximize_window()

driver.get('https://www.betstar.com.au/sports/soccer/44317884-football-scotland-ladbrokes-league-2/')


# XPaths for the data
groups = "//*[@class='fullbox']"

xp_bp1 = ".//*[@class='row'][1]//td[1]"
xp_ba1 = ".//*[@class='row'][2]//td[1]"
xp_bp3 = ".//*[@class='row'][1]//span"

while True:
    try:
        # wait for the data to populate the tables
        #wait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, (xp_bp1))))
        time.sleep(2)

        data = []
        for elem in driver.find_elements_by_xpath(groups):
            try:
                bp1 = elem.find_element_by_xpath(xp_bp1).text
            except:
                bp1 = None

            try:
                ba1 = elem.find_element_by_xpath(xp_ba1).text
            except:
                ba1 = None

            try:
                bp3 = elem.find_element_by_xpath(xp_bp3).text
            except:
                bp3 = None


            url1 = driver.current_url

            data.append([bp1, ba1, bp3, url1])
        print(data)
        url1 = driver.current_url
        with open('test.csv', 'a', newline='', encoding="utf-8") as outfile:
            writer = csv.writer(outfile)
            for row in data:
                writer.writerow(row + (url1,))

    except TimeoutException as ex:
        pass
    except NoSuchElementException as ex:
        print(ex)
        break
答案

你有data.append([bp1, ba1, bp3, url1]),这意味着当你这样做

    for row in data:
        writer.writerow(row + (url1,))

row已经包含了url1。您希望4个数据元素出现在csv中,因此请将代码更改为:

for row in data:
    writer.writerow(row)
另一答案

(url1,)它创建一个元组并导致问题,因此修复替换

with open('test.csv', 'a', newline='', encoding="utf-8") as outfile:
    writer = csv.writer(outfile)
    for row in data:
        writer.writerow(row + (url1,))

使用以下代码:

(也没有必要做+ (url1,),因为它已经附加到数据data.append([bp1, ba1, bp3, url1])

with open('test.csv', 'a', newline='', encoding="utf-8") as outfile:
    writer = csv.writer(outfile)
    for row in data:
        writer.writerow("	".join(row))

以上是关于将当前url写入csv TypeError时:只能将列表(不是“元组”)连接到列表的主要内容,如果未能解决你的问题,请参考以下文章

TypeError: to_csv() 得到了一个意外的关键字参数“startrow”

TypeError:尝试绘制函数时,只能将大小为 1 的数组转换为 Python 标量

Python 3.3 CSV.Writer 写入额外的空白行

python csv writer用科学计数法写入数组

TypeError:连接 csv 文件时,“str”对象不是迭代器

使用循环进行 Web 抓取并写入 csv