Не получается получить доступ к сайту, блокируется остальной код, парсинг

import requests 
from bs4 import BeautifulSoup 
import requests as req 
import pandas as pd 
import xlsxwriter 
import time 
 
new = time.asctime() 
 
 
def goParse(): 
 page_num = 1 
 URL = 'https://jut.su/anime/page-1' 
 HEADERS = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36 OPR/72.0.3815.487' 
 titles, links, times, hyperlinks = [], [], [], [] 
 
 while page_num < 2: 
 print(page_num) 
 res = requests.get(URL, HEADERS) 
 print(res) 
 html = BeautifulSoup(res.text, 'lxml') 
 series = html.find_all('span', class_='aailines') 
 links_a = html.find_all('a') 
 page = html.find_all('a', class_='vnright') 
 titles = html.find_all('div', class_='aaname') 
 if page == 'None': 
 break 
 else: 
 page_num += 1 
 
 df = pd.DataFrame() 
 df['Серии'] = series 
 df['заголовки'] = titles 
 df['ссылки'] = links_a 
 way = f'./jutsu{new}.xlsx'.replace(':', '_') 
 writer = pd.ExcelWriter(way, engine='xlsxwriter') 
 df.to_excel(writer, sheet_name='лист 1', index=False) 
 writer.sheets['лист 1'].set_column('A:A', 20) 
 writer.sheets['лист 1'].set_column('B:B', 140) 
 writer.save() 
 
 
goParse()

Response 403, через postman перепроверял, работает и без юзер агента.


Ответы (1 шт):

Автор решения: Jack_oS

Заголовки задаются не строкой, а словарем с ключем - названием заголовка, поэтому:

HEADERS = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36 OPR/72.0.3815.487'
}

И requests.get() принимает этот заголовок не позиционным

>>> res = requests.get(URL, HEADERS)
>>> res
<Response [403]>

а именованым параметром:

>>> res = requests.get(URL, headers=HEADERS)
>>> res
<Response [200]>

Результат res.content:

b'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru" lang="ru">\r\n<head>\r\n    <meta http-equiv="Content-Type" content="text/html; charset=windows-1251"/>\r\n<title>\xd1\xec\xee\xf2\xf0\xe5\xf2\xfc \xe0\xed\xe8\xec\xe5 \xed\xe0 Jut.su</title>\r\n<meta name="description" content="\xc2\xf1\xe5 \xf1\xe5\xf0\xe8\xe8 \xe8 \xf1\xe5\xe7\xee\xed\xfb \xec\xed\xee\xe6\xe5\xf1\xf2\xe2\xe0 \xe0\xed\xe8\xec\xe5 \xe8 \xec\xf3\xeb\xfc\xf2\xf4\xe8\xeb\xfc\xec\xee\xe2. \xcb\xf3\xf7\xf8\xe8\xe5 \xe0\xed\xe8\xec\xe5, \xef\xee\xf1\xec\xee\xf2\xf0\xe5\xf2\xfc \xea\xee\xf2\xee\xf0\xfb\xe5 \xec\xee\xe6\xed\xee \xf3 \xed\xe0\xf1."/>\r\n<meta name="keywords" content="\xec\xf3\xeb\xfc\xf2\xf4\xe8\xeb\xfc\xec\xfb, \xe0\xed\xe8\xec\xe5, \xf1\xec\xee\xf2\xf0\xe5\xf2\xfc, \xee\xed\xeb\xe0\xe9\xed, \xe2\xe8\xe4\xe5\xee, \xf1\xe5\xf0\xe8\xe8, \xf1\xe5\xe7\xee\xed\xfb, \xfd\xef\xe8\xe7\xee\xe4\xfb"/>\r\n<link rel="canonical" href="https://jut.su/anime/"/>\r\n    

UPD без User-Agent этот сайт не отдает 200:

>>> res = requests.get(URL)
>>> res
<Response [403]>
→ Ссылка