“在解析时出现意外的EOF”,同时存储在文件中

时间:2015-07-13 08:21:34

标签: python python-3.x beautifulsoup python-requests eof

我正在尝试从服务器解析数据并希望将其存储在文件中,但我在解析时遇到了意外的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')

请给我任何参考或提示。

1 个答案:

答案 0 :(得分:1)

您忘记添加结束括号。

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

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