python-3.x - 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:无
我之前已经检查了这些步骤,但是在找到查找功能之前一切正常。
我以前从来没有问过问题,如果我犯了错误,请原谅我。谢谢您的帮助。
解决方案
您必须使用不同的parser
尝试进行以下更改:
soup = BeautifulSoup(page.content, 'html.parser')
至
soup = BeautifulSoup(page.content, 'lxml')
推荐阅读
- python - PyCUDA 中的内存分配顺序是否重要?
- javascript - 数字显示为 NaN
- tensorflow - tf.keras.layers.Conv2D 获取内核值
- .net - 将字符串拆分为行并使用.net检查当前行和下一行
- laravel - 更新当前布尔值的相反值
- php - Javascript 时间戳在 Laravel 中被 Carbon 解析为错误日期
- php - 附加到 XML 的末尾时,formatOutput 不起作用,但在第一次创建文件时,formatOutput 工作正常
- css - 将反应门户居中在父元素的顶部
- r - muthist 中没有空格的盒子
- javascript - 在特定单元格不为空时在 Google 表格中运行一个脚本,将公式生成到单元格中