无法将一些表格内容写入文本文件

时间:2019-08-12 07:59:17

标签: python python-3.x web-scraping

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

Site link

我尝试过:

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)

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

1 个答案:

答案 0 :(得分:0)

此脚本会将表格数据保存到'\t'分隔的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中的结果:

enter image description here