【问题标题】:Python error - TypeError: object of type 'NoneType' has no len()Python 错误 - TypeError:“NoneType”类型的对象没有 len()
【发布时间】:2021-06-14 22:31:31
【问题描述】:

我想从以下网站阅读价格。 但是当我运行时,我得到了 [TypeError: object of type 'NoneType' has no len()] 错误。

word = '0735850859699'
driver = webdriver.Chrome('chromedriver.exe')
url = driver.get("https://www.lotteon.com/p/product/LM" + str(word))
time.sleep(3)

soup = BeautifulSoup(url, 'lxml')
content = soup.findAll("div", {"class":"price"}).text
print(content)

enter image description here

【问题讨论】:

  • findAll 返回一个没有 .text 属性的列表。
  • 你能给我们完整的错误文本吗?
  • 完全错误:回溯(最近一次调用最后一次):文件“c:\0_MINSU\PythonWorksapce\practice.py”,第 165 行,在 汤 = BeautifulSoup(url, 'lxml') init 中的文件“C:\Users\lemi7005\AppData\Local\Programs\Python\Python39\lib\site-packages\bs4_init_.py”,第 310 行elif len(markup)

标签: python


【解决方案1】:

您的 url 对象是来自 .get() 的 Nonetype。你想使用方法 .page_source:

from selenium import webdriver
from bs4 import BeautifulSoup

word = '0735850859699'
firefoxoptions = webdriver.FirefoxOptions()
firefoxoptions.set_headless()
driver = webdriver.Firefox(executable_path=r"<gecko_driver_path_here>", 
                           firefox_options=firefoxoptions)
driver.get(f"https://www.lotteon.com/p/product/LM{word}")
url = driver.page_source

soup = BeautifulSoup(url, 'lxml')
content = soup.find("div", {"class":"price"}).text
print(content)

【讨论】:

  • 感谢您的评论,顺便说一句,我应该使用 Firefox 浏览器吗?因为我的笔记本电脑里只有 chrome 浏览器..!
  • 两者都可以正常工作,我只是碰巧有 Firefox。请注意 b/c 选项设置与 Chrome 略有不同。
猜你喜欢
  • 1970-01-01
  • 2021-03-03
  • 2015-07-30
  • 1970-01-01
  • 1970-01-01
  • 2016-06-06
  • 2012-08-02
  • 2018-08-14
相关资源
最近更新 更多