0

I've written a script in python to get some tabular content from a webpage and my script can parse them accordingly. However, the problem is I can't write them to a text file. When I try to write, the script throws an error pointing at the very last line TypeError: write() argument must be str, not list.

Site link

I've tried with:

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)

How can I write the tabular data to a text file?

MITHU
  • 253
  • 2
  • 8
  • 28

1 Answers1

0

This script will save the tabular data to '\t'-separated csv file.

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')

The result in LibreOffice:

enter image description here

Andrej Kesely
  • 81,807
  • 10
  • 31
  • 56