因此,我有一个问题,我的网站刮板我正在开发。我遇到的主要问题是,当试图为h1格式的产品获取标题时,它会不断响应:
<h1 class="product-detail__title small-title">CHERRY MX SILENT RED(10pcs)</h1>
我只想要樱桃Mx无声的红色部分,而不是所有其他的东西。
以下是我的网络刮板的代码:
from bs4 import BeautifulSoup
URL = 'https://kbdfans.com/collections/cherry-switches/products/cherry-mx-silent-red'
headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'}
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find('h1', {'class' : 'product-detail__title small-title'})
print(title)