使用MechanicalSoup将文本文件上载到站点进行分析

2024-06-01 08:45:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将一个文本文件发送到TreeTagger Online以对其进行分析,并获取指向要下载的结果文件的链接。在

import mechanicalsoup  
browser = mechanicalsoup.Browser()
homePage = browser.get("http://cental.fltr.ucl.ac.be/treetagger/")
formPart = homePage.soup.select("form[name=treetagger_form]")[0]
formPart.select("[name=file_to_tag]")[0]["name"]=open('test.txt', 'rb')
result = browser.post(formPart, homePage.url)

这给了我以下错误:

: (, UnicodeEncodeError('ascii', u'No connection adapters were found for \'\n\n\n\n\n Texte \xe0 \xe9tiqueter : \n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\'', 216, 217, 'ordinal not in range(128)'))

我应该如何在站点上获取我的文件(使用MechanicalSoup或其他模块)?在


01/04/19编辑

尽管我没能得到@Rolando Urquiza在我的机器上的答案,但我还是能从他的建议中完成这件事。在

^{pr2}$

谢谢@Rolando Urquiza


Tags: 文件namebrowserformselectonline指向homepage
1条回答
网友
1楼 · 发布于 2024-06-01 08:45:04

根据MechanicalSoup的文档,您可以使用set函数在mechanicalsoup.Form实例上上载文件,请参见here。例如,您可以使用它:

import mechanicalsoup  
browser = mechanicalsoup.StatefulBrowser()
browser.get("http://cental.fltr.ucl.ac.be/treetagger/")
form = browser.select_form()
form.set('file_to_tag', 'test.txt')
result = browser.submit_selected()

相关问题 更多 >