我已经写了我的第一个python代码来抓取一个网站。Python - AttributeError:'NoneType'对象没有属性'findAll'
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)
当我在我的IDE,称为pyCharm它的作品好,但是当我尝试一下它运行CentOS的我的服务器上,我碰到下面的错误进行本地测试:
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'
我猜我没有远程安装模块,我已经挂了两天,任何帮助将不胜感激! :)
@karthikr:这不是问题所在;他正在尝试调用'table.findAll',因为_table_已经是'None'。 – abarnert
为了调试这个,尝试打印'soup',和'soup.find('table')',和'soup.find('table',id =“datatable_main”)',看看它们是什么样子。 – abarnert
此外,我注意到您的回溯中的行和源中的行不匹配。您是否有可能将较旧(并且尚未运行)的脚本版本上传到服务器箱? – abarnert