晚上好,以下代码未返回任何内容,但是我希望看到产品标题。我认为标题变量不正确,但是我不确定需要修改什么。
任何帮助都很好,
谢谢
import requests
from bs4 import BeautifulSoup
URL = 'https://www.amazon.co.uk/Razer-BlackWidow-Lite-Backlighting-Productivity/dp/B07KQR7RLH/ref=sr_1_1?crid=33KIPEXJU7JNK&dchild=1&keywords=blackwidow+lite&qid=1592502185&sprefix=blackwidow%2Caps%2C158&sr=8-1'
headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'}
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
# print(soup.prettify())
title = soup.find(id="productTitle")
print(title)
答案 0 :(得分:0)
也许可以尝试一下,可能无法在不知道ID的位置找到id productTitle。
title = soup.find('span', {'id':'productTitle'})
您也可以尝试:
titleSection = soup.find('div', class_='a-section a-spacing-none')
title = titleSection.h1.span.text