繁体   English   中英

使用 BeautifulSoup 获取所有按钮的列表,但未显示所需按钮

[英]Using BeautifulSoup to get list of all buttons, but needed buttons are not displaying

我正在使用 BeautifulSoup 来获取页面上所有按钮对象的列表,以便我可以选择一个稍后与之交互。 我正在使用下面的代码,但在网站上没有显示添加到购物车按钮。 如果我检查元素,我可以看到它是一个按钮。

我试图以尽可能通用的方式做到这一点,以便我可以为多个网站使用相同的代码。 因此,理想情况下,我不想使用特定属性来查找该按钮,我想创建一个 function,它允许我通过显示所有按钮并随后缩小范围来轻松找到任何页面上的任何所需按钮。

知道为什么我无法在此代码中找到添加到购物车按钮吗?

from bs4 import BeautifulSoup

page = requests.get('https://www.target.com/p/madden-nfl-21-xbox-one-series-x/-/A-79800769')
data = page.text
soup = BeautifulSoup(data, 'html.parser')
buttons = soup.find_all('button')
for button in buttons:
    print(button)

您将需要selenium因为按钮是由 JS 生成的。 请阅读答案以获取更多解释。

代码:

from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.chrome.options import Options

# start chrome in headless (not visible) mode
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)

# go to url and wait until page is loaded
url = 'https://www.target.com/p/madden-nfl-21-xbox-one-series-x/-/A-79800769'
driver.get(url)

# get html of page and close webdriver
html = driver.page_source
driver.quit()

# do whatever you want with buttons
soup = BeautifulSoup(html, 'html.parser')
buttons = soup.find_all('button')
print(*buttons, sep='\n')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM