Без головы не работает с помощью драматурга и Beautifulsoup 4Python

Программы на Python
Anonymous
 Без головы не работает с помощью драматурга и Beautifulsoup 4

Сообщение Anonymous »

Этот код работает: < /p>
from playwright.sync_api import sync_playwright
from bs4 import BeautifulSoup
from datetime import datetime
import time

with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
page = browser.new_page()
page.goto("https://www.apple.com/br/shop/product/M ... de-recarga")
html = page.content()
soup = BeautifulSoup(html,'html.parser')
valorAppleStore = soup.select("span.as-price-installments")[-2].get_text().replace(" à vista (10% de desconto)", '')
print(valorAppleStore)
browser.close()

Но если я изменяю Headless = true , код возвращает ошибку:
Traceback (most recent call last):
File "c:/Users/ANDERSONCARVALHODELI/Documents/py/AirpodsPW.py", line 19, in
valorAppleStore = soup.select("span.as-price-installments")[-2].get_text().replace(" à vista (10% de desconto)",
'')
IndexError: list index out of range
< /code>
Я исправил это с помощью: < /p>
from playwright.sync_api import sync_playwright
from bs4 import BeautifulSoup
from datetime import datetime
import time

with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
page = browser.new_page()
page.goto("https://www.apple.com/br/shop/product/M ... de-recarga")
time.sleep(1)
browser.close()

browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto("https://www.apple.com/br/shop/product/M ... de-recarga")
html = page.content()
soup = BeautifulSoup(html,'html.parser')
valorAppleStore = soup.select("span.as-price-installments")[-2].get_text().replace(" à vista (10% de desconto)", '')
print(valorAppleStore)
< /code>
Но я думаю, что это не лучший выбор. Как исправить это без открытия браузера с помощью Headless = false и придерживаться Headless = true ?
Когда я печатаю (html) перед Суп = ... , я вижу:
Page Not Found - Apple [*] .more::after{content: "";}a.pointer, a.more, a.block span.more, button.unbutton.more{padding-right: .7em; background-image: url(Изображение); background-repeat: no-repeat; background-position: 100% 50%; background-size: 5px 9px; zoom: 1;}.as-globalfooter-directory-column-section-list a{margin-bottom: .8em; display: block}.as-globalfooter-directory-column-section-list a:last-child{margin-bottom: 0;}.as-globalfooter-mini .as-globalfooter-mini-shop a{color: #06c;}.as-globalfooter .as-globalfooter-mini-legal-copyright, .as-footnotes .as-globalfooter-mini-legal-copyright, .as-globalfooter .as-globalfooter-mini-legal-link, .as-footnotes .as-globalfooter-mini-legal-link{top: -3px; position: relative; z-index: 1;}.as-globalfooter .as-globalfooter-directory+.as-globalfooter-mini, .as-footnotes .as-globalfooter-directory+.as-globalfooter-mini{padding-bottom: 26px;}.container{position: relative;}hr{display: inline-block; border: 0px; border-top: 0.1em solid #CCD2D9; width: 100%} The page you’re looking for can’t be found. Or see our site map

Apple
  • Page Not Found
Shop and Learn Services Account Apple Store For Business For Education For Healthcare For Government Apple Values About Apple More ways to shop:
Find an Apple Store or other retailer near you. Or call 1-800-MY-APPLE. United States
Copyright © 2022 Apple Inc. All rights reserved.
Privacy Policy Terms of Use Sales
and Refunds
Legal Site Map


Подробнее здесь: https://stackoverflow.com/questions/728 ... ifulsoup-4

Вернуться в «Python»