简体   繁体   English

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

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

I'm using BeautifulSoup to get a list of all button objects on a page, so that I can pick one to later interact with.我正在使用 BeautifulSoup 来获取页面上所有按钮对象的列表,以便我可以选择一个稍后与之交互。 I'm using the below code, but on the website the add to cart button isn't showing up.我正在使用下面的代码,但在网站上没有显示添加到购物车按钮。 If I do inspect element, I can see that it is a button.如果我检查元素,我可以看到它是一个按钮。

I am attempting to do this in as universal a way as possible, so that I can use the same code for multiple websites.我试图以尽可能通用的方式做到这一点,以便我可以为多个网站使用相同的代码。 So ideally I don't want to use specific attributes to find that button, I want to create a function that allows me to easily find any needed button on any page by displaying all of them and later narrowing it down.因此,理想情况下,我不想使用特定属性来查找该按钮,我想创建一个 function,它允许我通过显示所有按钮并随后缩小范围来轻松找到任何页面上的任何所需按钮。

Any idea why I'm not able to find the add to cart button in this code?知道为什么我无法在此代码中找到添加到购物车按钮吗?

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)

You will need selenium because buttons are generated by JS.您将需要selenium因为按钮是由 JS 生成的。 Please read this answer for more explanations.请阅读答案以获取更多解释。

Code:代码:

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