首页 > 解决方案 > 将一些表格内容写入文本文件时遇到问题

问题描述

我用 python 编写了一个脚本来从网页中获取一些表格内容,我的脚本可以相应地解析它们。但是,问题是我无法将它们写入文本文件。当我尝试编写时,脚本会抛出指向最后一行的错误TypeError: write() argument must be str, not list

网站链接

我试过:

import requests
from bs4 import BeautifulSoup

url = "https://en.wikipedia.org/wiki/Comparison_of_Intel_processors"

res = requests.get(url)
soup = BeautifulSoup(res.text, "lxml")

with open("tabular_content.txt", "w", newline="", encoding="UTF-8") as outfile:              
    for items in soup.find("table",class_="wikitable").find_all("tr"):
        data = [item.get_text(strip=True) for item in items.find_all(["th","td"])]
        print(data)
        outfile.write(data)

如何将表格数据写入文本文件?

标签: pythonpython-3.xweb-scraping

解决方案


此脚本会将表格数据保存到'\t'-separated csv 文件。

import requests
from bs4 import BeautifulSoup

url = "https://en.wikipedia.org/wiki/Comparison_of_Intel_processors"

res = requests.get(url)
soup = BeautifulSoup(res.text, "lxml")

with open("file.csv", "w", newline="", encoding="UTF-8") as outfile:
    for items in soup.find("table",class_="wikitable").find_all("tr"):
        data = [item.get_text(strip=True).replace('\n', ' ') for item in items.find_all(["th","td"])]
        print(data)
        outfile.write('\t'.join(data) + '\n')

LibreOffice 中的结果:

在此处输入图像描述


推荐阅读