Парсер на python не обходит несколько страниц на сайте, а зацикливается на одной в цикле while
import requests
from bs4 import BeautifulSoup as BS
p = 1
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
}
while True:
url = "https://global.pantum.com/product-center/p-" + str(p) + "/#content"
page = requests.get(url, headers=headers)
save_data = page.text
with open("index.html", "w", encoding="utf-8") as file:
file.write(save_data)
with open("index.html") as file:
data = file.read()
soup = BS(data, 'html.parser')
is_new_hot_device = soup.find("div", class_="pro_list cf").find_all("span")
printers = soup.find("div", class_="pro_list cf").find_all("dd")
list(is_new_hot_device)
list(printers)
if len(printers):
for i in range(0, len(printers)):
if i >= len(is_new_hot_device):
print(printers[i].text.strip())
else:
print(printers[i].text.strip() + " " + is_new_hot_device[i].text.strip().upper())
p = +1
else:
break
#print(requests.get(url, headers=headers))
Ответы (1 шт):
Автор решения: CrazyElf
→ Ссылка
Довольно типовая ошибка:
p = +1
Здесь вы просто присваиваете в p единичку каждый раз.
А нужно так:
p += 1