【发布时间】:2021-10-28 22:17:42
【问题描述】:
所以我在为我正在开发的网站制作的网络爬虫时遇到问题。我遇到的主要问题是,当尝试获取 h1 格式的产品的标题时,它会不断响应:
<h1 class="product-detail__title small-title">CHERRY MX SILENT RED(10pcs)</h1>
我只想要 Cherry Mx Silent Red 部分,而不是所有其他东西。
这是我的网络爬虫的代码:
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)
【问题讨论】:
标签: html web-scraping beautifulsoup