gpt4 book ai didi

python - Beautifulsoup的语法错误

转载 作者:行者123 更新时间:2023-12-03 08:29:19 24 4
gpt4 key购买 nike

我已经阅读了很多文档,但是他们没有提供这样的示例。我已经尝试调试了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()
>>>

最佳答案

在python3中,您需要将print视为一个函数:

print(tag['firstname'])

代替
print tag['firstname']

看到这个 Python3 release notes:

The print statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old print statement

关于python - Beautifulsoup的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24236852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com