首页 > 解决方案 > BeautifulSoup find() 没有返回

问题描述

import requests
from bs4 import BeautifulSoup

URL = 'https://www.amazon.de/BenQ-GL2580H-Monitor-Eye-Care-Reaktionszeit/dp/B073NTJHYY/ref=sr_1_3?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=bildschirm&qid=1597391122&sr=8-3'


headers = {
    "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36'
}

page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')

title = soup.find(id="productTitle")
price = soup.find(id="priceblock_ourprice")

print("Titel:",title,"\n","Preis:",price)

输出总是: 标题:无 Preis:无

我之前已经检查了这些步骤,但是在找到查找功能之前一切正常。

我以前从来没有问过问题,如果我犯了错误,请原谅我。谢谢您的帮助。

标签: python-3.xbeautifulsoup

解决方案


您必须使用不同的parser 尝试进行以下更改:

soup = BeautifulSoup(page.content, 'html.parser')

soup = BeautifulSoup(page.content, 'lxml')

推荐阅读