import requests
from bs4 import BeautifulSoup
import csv
url = "https://"
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
data = []
for tag in soup.find_all(['div', 'table']):
if tag.name == 'div' and len(tag.find_all('table')) > 0:
for subtag in tag.find_all('table'):
data.append(subtag.text.strip())
with open('output.csv', 'w') as file:
writer = csv.writer(file)
for item in data:
writer.writerow([item])
writer.writerow([item])
writer.writerow([item])
try:
with open('output.txt', 'r') as file:
contents = file.read()
print(contents)
except FileNotFoundError:
print("File not found")
except Exception as e:
print(f"An error occurred: {e}")
The code is trying to access the name attribute of each tag, but when it encounters a NavigableString, it raises an error.
New contributor
Reonard1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.