Код: Выделить всё
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}")
Подробнее здесь: https://stackoverflow.com/questions/786 ... b-scraping