Дело в том, что если парсить примерно страниц 10, то ошибки не вылетает, все работает хорошо. Но если заставлять парсить все страницы, то вылетает вот эта ошибка. В чем может быть проблема, не могу понять.
import requests
from bs4 import BeautifulSoup
import csv
def get_hmtl(url):
respons = requests.get(url)
return respons.text
def get_total_pages (html):
soup = BeautifulSoup(html, 'lxml')
pages = soup.find('div', class_='pagination-pages clearfix').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('D:/python/avito.csv', 'a') as file:
writer = csv.writer(file)
writer.writerow(( data['title'],
data['price'],
data['metro'],
data['url'] ))
def get_page_date(html):
soup = BeautifulSoup(html, 'lxml')
ads = soup.find('div', class_='js-catalog_after-ads').find_all('div', class_='item item_table clearfix js-catalog-item-enum c-b-0')
for ad in ads:
#title, place, url, price
try:
title = ad.find('div', class_='description').find('h3').text.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_='description').find('div', class_='about').text.strip()
except:
price = ''
try:
place = ad.find('div', class_ = 'data').find_all('p')[-1].text.strip()
except:
place = ''
data = {
'title':title,
'price':price,
'metro':place,
'url':url
}
write_csv(data)
def main ():
# https://www.avito.ru/moskva/telefony?p=98&q=htc
url = 'https://www.avito.ru/moskva/telefony?p=98&q=htc'
base_url = 'https://www.avito.ru/moskva/telefony?'
page_part = 'p=' #
query_part = '&q=htc'
total_pages = get_total_pages(get_hmtl(url))
for i in range (1, total_pages):
url_gen = base_url + page_part + str(i) + query_part
html = get_hmtl(url_gen)
get_page_date(html)
if __name__ == '__main__':
main()