我正在从网站上抓取RSS源(http://www.gfrvitale.altervista.org/index.php/autismo-in?format=feed&type=rss)。 我已经写下了一个脚本来提取和纯化来自每个Feed的文本。我的主要问题是将每个项目的每个文本保存在一个不同的文件中,我还需要为每个文件命名该项目的正确标题exctractet。 我的代码是:如何在多个文件中保存多个输出,其中每个文件都有不同的标题来自python中的对象?
for item in myFeed["items"]:
time_structure=item["published_parsed"]
dt = datetime.fromtimestamp(mktime(time_structure))
if dt>t:
link=item["link"]
response= requests.get(link)
doc=Document(response.text)
doc.summary(html_partial=False)
# extracting text
h = html2text.HTML2Text()
# converting
h.ignore_links = True #ignoro i link
h.skip_internal_links=True #ignoro i link esterni
h.inline_links=True
h.ignore_images=True #ignoro i link alle immagini
h.ignore_emphasis=True
h.ignore_anchors=True
h.ignore_tables=True
testo= h.handle(doc.summary()) #testo estratto
s = doc.title()+"."+" "+testo #contenuto da stampare nel file finale
tit=item["title"]
# save each file with it's proper title
with codecs.open("testo_%s", %tit "w", encoding="utf-8") as f:
f.write(s)
f.close()
的错误是:
File "<ipython-input-57-cd683dec157f>", line 34 with codecs.open("testo_%s", %tit "w", encoding="utf-8") as f:
^
SyntaxError: invalid syntax
我做到了,但它不工作,我得到这个错误:C:\ Anaconda2 \ LIB \编解码器.pyc in open(filename,mode,encoding,errors,buffering) 894#以二进制模式强制打开文件 895 mode = mode +'b' - > 896 file = __builtin __。open(filename,mode,缓冲) 897 if encoding is None: 898 return file IOError:[Errno 22] invalid mode('wb')or filename:u'testo_La Comunicazione Facilitata? Parliamone。 – CosimoCD
代码是正确的。逗号在目标'%tit'之后,而不是之前。那是另一个错误。我会检查。 – estebanpdl
期望的输出是什么? (即'.csv','.txt') – estebanpdl