Python-AttributeError:"NoneType"对象没有属性"findAll" [英] Python - AttributeError: 'NoneType' object has no attribute 'findAll'

查看:325
本文介绍了Python-AttributeError:"NoneType"对象没有属性"findAll"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了第一段Python代码来抓取网站.

I have written my first bit of python code to scrape a website.

import csv
import urllib2
from BeautifulSoup import BeautifulSoup

c = csv.writer(open("data.csv", "wb"))
soup = BeautifulSoup(urllib2.urlopen('http://www.kitco.com/kitco-gold-index.html').read())
table = soup.find('table', id="datatable_main")
rows = table.findAll('tr')[1:]

for tr in rows:
   cols = tr.findAll('td')
   text = []
   for td in cols:
       text.append(td.find(text=True))
   c.writerow(text)

当我在名为pyCharm的本地环境中对其进行测试时,它运行良好,但是当我在运行CentOS的服务器上对其进行尝试时,出现以下错误:

When I test it locally in my ide called pyCharm it works good but when I try it out on my server which runs CentOS, I get the following error:

domainname.com [~/public_html/livegold]# python scraper.py
Traceback (most recent call last):
  File "scraper.py", line 8, in <module>
    rows = table.findAll('tr')[:]
AttributeError: 'NoneType' object has no attribute 'findAll'

我猜我没有远程安装模块,我已经挂了两天了,任何帮助将不胜感激! :)

I'm guessing I don't have a module installed remotely, I've been hung up on this for two days any help would be greatly appreciated! :)

推荐答案

如果您由于某种原因在尝试获取服务器上的该页面时遇到错误,则忽略了urllib2.urlopen中可能发生的任何错误.不要在本地进行测试,您实际上是将空字符串('')或您不希望看到的页面(例如404页面)传递给BeautifulSoup.

You are ignoring any errors that could occur in urllib2.urlopen, if for some reason you are getting an error trying to get that page on your server, which you don't get testing locally you are effectively passing in an empty string ('') or a page you don't expect (such as a 404 page) to BeautifulSoup.

由于文档不是您所期望的,因此又使您的soup.find('table', id="datatable_main")返回None.

Which in turn makes your soup.find('table', id="datatable_main") return None since the document is something you don't expect.

您应该确保可以获取要在服务器上获取的页面,或者可以正确处理异常.

You should either make sure you can get the page you are trying to get on your server, or handle exceptions properly.

这篇关于Python-AttributeError:"NoneType"对象没有属性"findAll"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆