Форум сайта python.su
Всем добрый день! Всю голову сломал, написал парсер школы, а вот написать работающую функцию для сохранения в csv файл не могу, помогите!!!!
import requests from bs4 import BeautifulSoup import csv URL = 'https://s-109.edusite.ru/sveden/employees.html' HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/86.0.4240.80 Safari/537.36 Edg/86.0.622.48', 'accept': '*/*'} FILE = 'teachers.csv' def get_html(url,params=None): r = requests.get(url,headers=HEADERS,params=params) r.encoding = 'utf8' return r def parse(): html = get_html(URL) get_content(html.text) def get_content(html): soup = BeautifulSoup(html,'html.parser') teaсhers = soup.find_all('div',class_='group-title') school = for teaсher in teaсhers: school.append({'ФИО преподавателя': teaсher.text}) school.append({'TOTAL': len(school)}) save(school,FILE) def save(school, path): with open(path, 'w', newline='') as file: writer = csv.writer(file, delimiter=';') writer.writerow(]) parse()
Офлайн
def get_content(html): soup = BeautifulSoup(html,'html.parser') teachers = soup.find_all('div',class_='group-title') school = # Что здесь? for teacher in teachers: school.append({'ФИО преподавателя': teacher.text}) school.append({'TOTAL': len(school)}) save(school,FILE) def save(school, path): with open(path, 'w', newline='') as file: writer = csv.writer(file, delimiter=';') writer.writerow(]) # Что это в скобках? parse()
Отредактировано FishHook (Окт. 24, 2020 11:42:16)
Офлайн
FishHookПочему-то неправильно скопировал
import requests from bs4 import BeautifulSoup import csv URL = 'https://s-109.edusite.ru/sveden/employees.html' HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/86.0.4240.80 Safari/537.36 Edg/86.0.622.48', 'accept': '*/*'} FILE = 'teachers.csv' def get_html(url,params=None): r = requests.get(url,headers=HEADERS,params=params) r.encoding = 'utf8' return r def parse(): html = get_html(URL) get_content(html.text) def get_content(html): soup = BeautifulSoup(html,'html.parser') teaсhers = soup.find_all('div',class_='group-title') school = [] for teaсher in teaсhers: school.append({'ФИО преподавателя': teaсher.text}) school.append({'TOTAL': len(school)}) save(school,FILE) def save(school, path): with open(path, 'w', newline='') as file: writer = csv.writer(file, delimiter=';') writer.writerow([school['title']]) parse()
Офлайн