Как спарсить ajax на python 3.

Published: 21 May 2021
on channel: Nikolay
5,207
161

Мой личный сайт (с советами по разработке):
https://dewhole.xyz

Мой интернет-магазин аккумуляторов с доставкой по всей России:
https://фурукава.рф

Готовый код из этого видео:

import requests
from bs4 import BeautifulSoup
import csv
import time
import urllib
import fake_useragent

HEADERS = {
'Host': 'mail.ru',
'Pragma': 'no-cache',
'Referer': 'https://mail.ru/',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"',
'sec-ch-ua-mobile': '?0',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
}






def get_html(url, params=None):
r = requests.get(url, headers=HEADERS, params=params)
return r


def get_pages_count(html):
soup = BeautifulSoup(html, 'html.parser')
paginationTo = soup.find('div', class_='nums')
if paginationTo:
paginationTo = soup.find('div', class_='nums')
pagination = paginationTo.find_all('a')
return int(pagination[-1].get_text())
else:
return 1





def get_content(html):
soup = BeautifulSoup(html, 'html.parser')


items = soup.find_all('a')

catalog = []
for item in items:
item = str(item)
a = item.find("svelte-1kcqj27")
if a != -1:
continue
print(item)





catalog.append({

})
return catalog




def save_file(items, path):
with open(path, 'w', encoding='utf8', newline='') as file:
writer = csv.writer(file, delimiter=',')
writer.writerow([])
for item in items:
writer.writerow([])

def parse():
for URL in [


'https://mail.ru/',


]:

html = get_html(URL)
if html.status_code == 200:
catalog = []
pages_count = get_pages_count(html.text)
for page in range (1, pages_count + 1):
print(f'Парсинг страницы {page} {pages_count} {URL}...')
html = get_html(URL, params={'PAGEN_1': page})
catalog.extend(get_content(html.text))
time.sleep(1)
FILE = 'parseResult' + '.csv'
save_file(catalog, FILE)


print(f'Получено {len(catalog)} товаров')
else:
print('Error')


parse()


On this page of the site you can watch the video online Как спарсить ajax на python 3. with a duration of hours minute second in good quality, which was uploaded by the user Nikolay 21 May 2021, share the link with friends and acquaintances, this video has already been watched 5,207 times on youtube and it was liked by 161 viewers. Enjoy your viewing!