首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Beautifulsoup中查找函数返回None

在Beautifulsoup中查找函数返回None
EN

Stack Overflow用户
提问于 2019-11-18 12:43:00
回答 1查看 71关注 0票数 0

我正在尝试从亚马逊获取和跟踪产品的价格,但我没有得到任何作为find函数的结果!

这是我的代码。我的错误是什么,我遗漏了什么?

代码语言:javascript
运行
复制
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)

感谢大家的帮助。

EN

回答 1

Stack Overflow用户

发布于 2019-11-18 13:39:08

问题解决了!以下是解决方案:

代码语言:javascript
运行
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58908073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档