"Unexpected EOF while parsing" 存储在文件中

"Unexpected EOF while parsing" while storing in file

我正在尝试解析来自服务器的数据并想将其存储在文件中,但在解析时出现意外的 EOF。我是 python 的新手。

这是我的代码。

import requests
from bs4 import BeautifulSoup
url = "http://www.couponindia.in/";
r= requests.get(url)
soup = BeautifulSoup(r.content,"html.parser")
g_data = soup.find_all("div", {"id" : "container"})

for item in g_data:
    print (item.text.encode('ascii','ignore'))
    with open('d:\test.txt', 'w') as f:
        f.write(item.text.encode('ascii','ignore')

请给我任何参考或提示。

您忘记添加右括号。

f.write(item.text.encode('ascii','ignore'))
                                          ^

with open(r'd:\test.txt', 'w') as f:
    f.write(item.text.encode('ascii','ignore'))