D
D
Denis2017-11-01 00:03:54
MySQL
Denis, 2017-11-01 00:03:54

What should module 'csv' has no attribute 'writer' do?

Here is the code:

import requests
from bs4 import BeautifulSoup
import csv



# План:
# 1. Выяснить кол-во страниц
# 2. Сформировать список урлов на страницы выдачи
# 3. Собрать данные

def get_html(url):
    r = requests.get(url)
    return r.text

def get_total_pages(html):
    soup = BeautifulSoup(html, 'lxml')
    pages = soup.find('div', class_='pagination-pages').find_all('a', class_='pagination-page')[-1].get('href')
    total_pages = pages.split('=')[1].split('&')[0]
    return int(total_pages)


def write_csv(data):
    with open('amigo.csv', 'a') as f:
        writer = csv.writer(f, delimiter=';', lineterminator='\n')

        writer.writerows( (data['title'],
                          data['price'],
                          data['metro'],
                          data['url']) )

def get_page_data(html):
    soup = BeautifulSoup(html, 'lxml')

    ads = soup.find('div', class_='catalog-list').find_all('div', class_='item_table')

    for ad in ads:
        #title, price, metro, url
        try:
            title = ad.find('div', class_='description').find('h3').text.encode('utf-8').strip()
        except:
            title = ''
        try:
            url = 'https://www.avito.ru' + ad.find('div', class_='description').find('h3').find('a').get('href')
        except:
            url = ''
        try:
            price = ad.find('div', class_='about').text.encode('utf-8').strip()
        except:
            price = ''
        try:
            metro = ad.find('div', class_='data').find_all('p')[-1].text.encode('utf-8').strip()
        except:
            metro = ''

        data = {'title': title,
                'price': price,
                'metro': metro,
                'url': url}

        write_csv(data)


# noinspection SyntaxError
def main():
    url = 'https://www.avito.ru/krasnodar/kvartiry/sdam/posutochno?p=1&s=101&user=1'
    base_url = 'https://www.avito.ru/krasnodar/kvartiry/sdam/posutochno?'
    page_part = 'p='

    total_pages = get_total_pages(get_html(url))
    for i in range(1, total_pages+1):
        url_gen = base_url + str(i) + page_part
       # print(url_gen)
        html = get_html(url_gen)
       # noinspection SyntaxError
        get_page_data(html)



if __name__ == '__main__':
    main()

Here is the error:
C:\Users\Denis>python C:\Users\Denis\Desktop\avito.py
Traceback (most recent call last):
File "C:\Users\Denis\Desktop\avito.py", line 81, in
main()
File "C:\Users\Denis\Desktop\avito.py", line 76, in main
get_page_data(html)
File "C:\Users\Denis\Desktop\avito.py", line 61, in get_page_data
write_csv(data)
File "C:\Users\Denis\Desktop\avito.py", line 25, in write_csv
writer = csv.writer(f, delimiter=';', lineterminator='\n')
AttributeError: module ' csv' has no attribute 'writer'

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Siverius, 2019-08-05
@Siverius

MySQL query optimization

D
Denis, 2017-11-01
@De__nis

Solved by reinstalling Python

S
Stanislav Pugachev, 2017-11-01
@Stqs

is there a csv.py file nearby? if yes, then it will be imported

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question