#python #web-scraping #python-requests
Вопрос:
Я хочу очистить исторические данные о коэффициентах с этой страницы: https://www.betexplorer.com/tennis/atp-singles/antwerp/fucsovics-marton-harris-lloyd-george/jebw7aKt/#ah
данные в браузере не отображаются, пока вы не наведете курсор мыши на конкретное закрывающее значение в таблице
Я попытался найти URL-адрес, который используется веб-сайтом для извлечения данных из консоли:
но при использовании запросов он не возвращает требуемые данные:
u = "https://www.betexplorer.com/archive-odds/57k4hx4p6e6x9czds1a3/18/"
import requests
session = requests.Session()
r = session.get(u)
print(r.status_code)
#return 404
print(r.text)
#return this: 'tt<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">ntt<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xmlns:fb="http://ogp.me/ns/fb#">nttt<head>ntttt<base href="https://www.betexplorer.com/">ntttt<title>Betexplorer.com - Page not found</title>ntttt<meta http-equiv="content-type" content="text/html; charset=utf-8">ntttt<style type="text/css">ntttttbody {nttttttbackground-color: #505A60;nttttttcolor: #444444;nttttttmargin: 0; padding: 0;nttttttfont-size: 12px;nttttttfont-family: Tahoma, Arial, Verdana, sans-serif;nttttt}nttttt#all {nttttttpadding-top: 1px;nttttttpadding-left: 10px;nttttt}nnttttt#main {nttttttwidth: 738px;nttttttpadding: 5px 5px 10px 15px;nttttttbackground-color:#F2F2F2;nttttttline-height: 20px;nttttt}nttttt#main h2 {nttttttmargin-top: 5px;nttttt}nttttt#footer {nttttttwidth: 743px;nttttttheight: 26px;nttttttpadding-left: 15px;nttttttpadding-top: 8px;nttttttbackground-color:#C7C7C7;nttttttfont-size: 11px;nttttt}ntttt</style>nttt</head>nttt<body>ntttt<div id="all">nttttt<div id="main">ntttttt<h2>Page not found</h2>nttttttnttttt</div>nttttt<div id="footer">nttttttCopyright 2003-12 by BetExplorer.comnttttt</div>ntttt</div>nttt</body>ntt</html>n '
r.json()
#return this error: JSONDecodeError: Expecting value: line 2 column 3 (char 3)
Ответ №1:
Вы должны добавить referer в заголовки
import requests
headers = {
'referer': 'https://www.betexplorer.com/tennis/atp-singles/antwerp/fucsovics-marton-harris-lloyd-george/jebw7aKt/',
}
response = requests.get('https://www.betexplorer.com/archive-odds/57k4hx2s5a4x0xds1a3/16/', headers=headers)
print(response.json())