Python 美化组出现语法错误

Python 美化组出现语法错误,python,python-3.x,syntax-error,beautifulsoup,Python,Python 3.x,Syntax Error,Beautifulsoup,我已经阅读了很多文档,但它们没有提供这样的示例。我已经试着调试了3个多小时,但仍然不起作用 import urllib.request, string, csv from bs4 import BeautifulSoup csvfile = open('people.csv', 'w') csvwriter = csv.writer(csvfile, dialect='excel') peoplefile = open('people.txt', 'r') soup = BeautifulS

我已经阅读了很多文档,但它们没有提供这样的示例。我已经试着调试了3个多小时,但仍然不起作用

import urllib.request, string, csv
from bs4 import BeautifulSoup

csvfile = open('people.csv', 'w')
csvwriter = csv.writer(csvfile, dialect='excel')

peoplefile = open('people.txt', 'r')
soup = BeautifulSoup(timfile.read())
tag = soup.marker

for tag in soup.find_all('marker'):
    print tag['firstname']
    data=[[tag['firstname'], tag['lastname']]]
    csvwriter.writerows(data)

csvfile.close()
peoplefile.close()
我从命令提示符处收到以下消息:

(everything before is fine)...
>>> tag = soup.marker
>>> for tag in soup.find_all('marker'):
...     print tag['firstname']
  File "<stdin>", line 2
    print tag['firstname']
            ^
SyntaxError: invalid syntax
>>>     data=[[tag['firstname'], tag['lastname']]]
  File "<stdin>", line 1
    data=[[tag['firstname'], tag['lastname']]]
    ^
IndentationError: unexpected indent
>>>     csvwriter.writerows(data)
  File "<stdin>", line 1
    csvwriter.writerows(data)
    ^
IndentationError: unexpected indent
>>>
... csvfile.close()
>>> peoplefile.close()
>>>
(之前的一切都很好)。。。
>>>tag=soup.marker
>>>用于汤中的标记。查找所有('marker'):
...     打印标签['firstname']
文件“”,第2行
打印标签['firstname']
^
SyntaxError:无效语法
>>>数据=[[tag['firstname'],tag['lastname']]
文件“”,第1行
数据=[[tag['firstname'],tag['lastname']]
^
缩进错误:意外缩进
>>>csvwriter.writerows(数据)
文件“”,第1行
csvwriter.writerows(数据)
^
缩进错误:意外缩进
>>>
... csvfile.close()
>>>peoplefile.close()
>>>

在python3中,您需要将打印作为一个函数:

print(tag['firstname'])
而不是

print tag['firstname']
见此:

print语句已替换为print()函数,该函数使用关键字参数替换旧print语句的大多数特殊语法


谢谢,这是什么意思?>>>print(tag['firstname'])Traceback(最近一次调用last):文件“”,第1行,在文件“C:\Python34\lib\site packages\beautifulsoup4-4.3.2-py3.4.egg\bs4\element.py”的第905行,在getitem keyrerror:'firstname'这意味着键
firstname
在字典中不存在
tag
try
print(tag.keys())
查看所有可用密钥。是否从python shell打印这些密钥?你需要把它放在汤中标签的
行后面。find_all('marker'):
OMG我在原始html文件中去掉了标签之间的一个额外的>。谢谢,我爱你!