【问题标题】:"Unexpected EOF while parsing" while storing in file存储在文件中时出现“解析时出现意外的 EOF”
【发布时间】:2015-07-13 08:21:33
【问题描述】:

我正在尝试解析来自服务器的数据并希望将其存储在文件中,但在解析时出现 Unexpected 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')

请给我任何参考或提示。

【问题讨论】:

  • 第 3 行末尾的 ; 的用途是什么?

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


【解决方案1】:

您忘记添加右括号。

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

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

【讨论】:

    猜你喜欢
    • 2013-04-25
    • 2021-03-21
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多