在Beautifulsoup中查找功能返回无

时间:2019-11-18 04:42:59

标签: python-3.x

我正在尝试从亚马逊获取并跟踪产品的价格,但是我没有作为查找功能结果!!

这是我的代码。我的错误是什么,我想念什么?

import requests
from bs4 import BeautifulSoup

URL = 'https://www.amazon.ca/Sony-WH1000XM3-Canceling-Headphones-WH-1000XM3/dp/B07G4MNFS1/ref=sr_1_3?keywords=sony+wh-1000xm3&qid=1574044279&sr=8-3'    
headers = {"User-Agent" : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 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(title)

感谢大家的帮助。

1 个答案:

答案 0 :(得分:0)

问题已解决!解决方法如下:

soup = BeautifulSoup(page.content, 'html.parser')
soup1 = BeautifulSoup(soup.encode("utf-16"),"html.parser")

title = soup1.find(id="productTitle").get_text()
price = soup1.find(id="priceblock_ourprice").get_text()

print(title.strip(), price)