【发布时间】:2020-07-25 10:45:57
【问题描述】:
我是学习 Python 并尝试制作网络爬虫应用程序的新手,但遇到了以下错误。这不是一个完整的代码,但如果这个问题没有首先解决,我将无法继续。
我们将不胜感激任何帮助!
AttributeError: 'NoneType' 对象没有属性 'get_text'
import requests
from bs4 import BeautifulSoup
url = "https://www.amazon.co.uk/b?node=13978643031&pf_rd_r=7WY9X56GFTSX0ZTD0VQQ&pf_rd_p=7510143e-2d7f-4e64-a435-f4e242b0abc4"
headers = {
"user-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"}
price = 389
def getPrice():
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find(id="productTitle").get_text().strip()
print(title)
if __name__ == "__main__":
getPrice()
错误:
Traceback(最近一次调用最后一次):文件 “/Users/sumeet/vs_code_py/app.py”,第 18 行,在 getPrice() 中 文件“/Users/sumeet/vs_code_py/app.py”,第 13 行,在 getPrice 标题 = soup.find(id="productTitle").get_text().strip() 属性错误: “NoneType”对象没有“get_text”属性
【问题讨论】:
-
这能回答你的问题吗? Web-scraping JavaScript page with Python
-
你知道是哪一行引发了错误吗?
-
你知道如何使用 Python 调试器吗?这是你现在必须学习的最重要的技能!
-
@ggorlen 感谢您的建议,但我是菜鸟,很难理解该解决方案。
-
@Marco 是的,这是信息。回溯(最后一次调用):文件“/Users/sumeet/vs_code_py/app.py”,第 18 行,在
getPrice() 文件“/Users/sumeet/vs_code_py/app.py”,第 13 行,在getPrice title = soup.find(id="productTitle").get_text().strip() AttributeError: 'NoneType' object has no attribute 'get_text'