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
.
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?