【发布时间】:2015-10-24 22:11:37
【问题描述】:
我正在尝试将漂亮的汤对象中的文本保存到文件中,以便以后编辑和使用。我已经导入了所有必要的模块,但由于某种原因,我每次在“pagename.write(str(soup))”处都会遇到相同的错误 我试过用多种方式重写,我只是难住了
#Testing implementation of writing to file
#save the HTML to a beautiful soup object
soup = BeautifulSoup(browser.page_source, 'html.parser')
#TODO: use breadcrumb of page name for loop later on
breadcrumb = soup.select('.breadcrumb span')
pagename = breadcrumb[0].get_text()
#open a file then write to it
bookPage = os.path.join('books/cpp/VST', pagename+'.txt')
open(pagename, 'wb')
pagename.write(str(soup))
#close file
#pagename.close()
#TODO: move on to next file
【问题讨论】:
-
pagename是字符串,不是文件对象
标签: python python-3.x beautifulsoup