【发布时间】:2017-01-11 14:40:05
【问题描述】:
每次运行以下 Python 脚本时,我都会收到错误 AttributeError: 'NoneType' object has no attribute 'findAll'。我做了一些研究,发现一些帖子表明我在尝试查找图像时可能会传递“无”,这就是它出错的原因。我仍然没有解决方案。任何信息都有帮助。
这是完整的错误:
Traceback (most recent call last):
File "D:\Program Files\Parser Python\Test.py", line 33, in <module>
for img in divImage.findAll('img'):
AttributeError: 'NoneType' object has no attribute 'findAll'
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.common.exceptions import TimeoutException
import os
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = 'C:\Program Files (x86)\Mozilla Firefox\firefox.exe'
os.environ["PATH"] += "C:\Python27\Lib\site-packages\selenium-2.53.6-py2.7.egg\selenium"
#binary = FirefoxBinary('C:\Program Files (x86)\Mozilla Firefox\firefox.exe')
driver = webdriver.Firefox(capabilities=firefox_capabilities)
# it takes forever to load the page, therefore we are setting a threshold
driver.set_page_load_timeout(5)
try:
driver.get("http://readcomiconline.to/Comic/Flashpoint/Issue-1?id=19295&readType=1")
except TimeoutException:
# never ignore exceptions silently in real world code
pass
soup2 = BeautifulSoup(driver.page_source, 'html.parser')
divImage = soup2.find('div', {"id": "divImage"})
#divImage = soup2.find('div', {"id": "containerRoot"})
# close the browser
driver.close()
for img in divImage.findAll('img'):
print img.get('src')
【问题讨论】:
-
这个话题对你有帮助吗?链接:stackoverflow.com/questions/18065768/…
-
这也可能与stackoverflow.com/questions/31419641/… 相关。顺便说一句,为什么要使用 Java 标记?
-
天哪,那是个意外。我最初试图用 Java 中的 JSOUP 来完成这个,所以我想我脑子里有 java 。我很抱歉。
-
有什么方法可以删除标签吗?
-
您应该可以在编辑问题时将其删除。此外,如果只是意外,也没问题。但是在stackoverflow上,有时有些人只是在他们的问题中添加尽可能多的语言标签,希望得到更快的答案,因此我的第一条评论。但正如我所说,如果这只是一个诚实的错误,那就忘掉它。看起来有人已经编辑了问题,只是在等待审核。