2016-04-18 73 views
1

我正在尝试读取网页并将格式化文本输出到文本文件。下面的代码使用格式打印到shell,但是当我将它写入文件时,它将它放在一行上(文本中出现换行符/ n)。与生成的文本文件相比,为什么Python 3 shell中的文本格式不同?

我尝试了各种各样的东西,如不将它转换为字符串,使用从美丽的汤美容但没有似乎产生格式文本文件。我假设我错过了一些相当基本的东西。任何帮助或指导将不胜感激。

# Import 
from urllib.request import urlopen 
from bs4 import BeautifulSoup 

#The actual code 


URL = "https://simple.wikipedia.org/wiki/castle" #The target URL 
html = urlopen(URL).read() # Reads the url to variable html 
soup = BeautifulSoup(html, "lxml") # Uses BS4 to create the soup using the lxml parser 
soup = soup.get_text() # Extracts the text 
print(soup) # Prints to python 3.5.1 shell, formatted as I would expect 


# Now writing what I have extracted to a text file 
file = open("TextOutput.txt", 'w') # Creates the file and opens as write (w) 
file.writelines(str(soup.encode('UTF-8'))) # Tried file.write/lines(soup), convertion to string and encoding as UTF-8 needed to avoid errors 
file.close() 

文件输出的一个例子是这样的:

B'\ n \ n \ nCastle - 简单的英文维基百科,自由的百科全书\ ndocument.documentElement.className = document.documentElement.className .replace(/(^ | \ s)client-nojs(\ s | $)/,“$ 1client-js $ 2”); \ n(window.RLQ = window.RLQ || [])。push(function ){mw.config.set({ “wgCanonicalNamespace”: “”, “wgCanonicalSpecialPageName”:假 “wgNamespaceNumber”:0 “wgPageName”: “城堡”, “wgTitle”: “城堡”, “wgCurRevisionId”:5333370, “wgRevisionId”:5333370“wgArticleId”:15933“wgIsArticle”:true,“wgIsRedirect”:false,“wgAction”:“view”,“wgUserName”:null,“wgUserGroups”:[“”], wgCategories “:” 城堡 “],” wgBreakFrames “:假的,” wgPageContentLanguage “:” 恩”, “wgPageContentModel”: “wikitext的”, “wgSeparatorTransformTable”: “”, “”], “wgDigitTransformTable”: “” “”], “wgDefaultDateFormat”: “日月”, “wgMonthNames”: “”, “月”, “日”, “月”, “月”, “五一”, “六一”, “七一”,”八五 “ ”九五“, ”十月“, ”月“, ”月“], ”wgMonthNamesShort“: ”“, ”月“, ”月“, ”月“, ”月“, ”月“,” 君“ ”月“, ”月“, ”月“, ”月“, ”月“, ”月“], ”wgRelevantPageName“: ”青山“, ”wgRelevantArticleId“:15933, ”wgRequestId“: ”VxUR5gpAIDAAAEXY6FMAAACC“,” wgIsProbablyEditable “:真实的,” wgRestrictionEdit “:[],” wgRestrictionMove “:[],” wgWikiEditorEnabledModules “:{” 工具栏 “:真正的” 对话 “:真正的” 预览 “:假的,” 出版 “:假},” wgBetaFeaturesFeatures “:[],” wgMediaViewerOnClick “:真” wgMediaViewerEnabledByDefault “:真” wgVisualEditor “:{” pageLanguageCode “:” EN”, “pageLanguageDir”: “LTR”, “usePageImages”:真 “usePageDescriptions”:真}, “wgPreferredVariant”: “恩”, “wgRelatedArticles”:空, “wgRelatedArticlesUseCirrusSearch”:真实的,“WG RelatedArticlesOnlyUseCirrusSearch “:假” wgULSAcceptLanguageList “:[],” wgULSCurrentAutonym “:” 英语 “ ”wgCategoryTreePageCategoryOptions“: ”{\“ 模式\ ”:0,\“ hideprefix \ ”:20,\“ showcount \”:真, \ “命名空间\”:假}”, “wgNoticeProject”: “维基百科”, “wgCentralNoticeCategoriesUsingLegacy”:[ “筹款”, “筹款”], “wgCentralAuthMobileDomain”:假 “wgWikibaseItemId”: “Q23413”, “wgVisualEditorToolbarScrollOffset”: 0}); mw.loader.implement( “user.options”,功能($,jQuery的){mw.user.options.set({ “变体”: “EN”});}); mw.loader.implement (“user.tokens”,函数($,jQuery){\ nmw.user.tokens.set({“editToken”:“+ \\”,“patrolToken”:“+ \\”,“watchToken”:“ \\“,”csrfToken“:”+ \\“});/ @nomin * /; \ n \ n}); mw.loader.load([”mw.MediaWikiPlayer.loader“,”mw.PopUpMediaTransform“ “mw.TMHGalleryHook.js”, “mediawiki.page.startup”, “mediawiki.legacy.wikibits”, “ext.centralauth.centralautologin”, “mmv.head”, “ext.visualEditor.desktopArticleTarget.init”,” ext.uls.init “ ”ext.uls.interface“,” ext.centralNot ice.bannerController”, “skins.vector.js”]);}); \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ nCastle \ n \ n来自维基百科,自由的百科全书\ n \ n \ n \ t \ t \ t \ t \ t跳转到:\ t \ \ t \ t \ t \ t导航,\ t \ t \ t \ t \ t搜索\ n \ n \ n \ n \ n \ n英国博迪安城堡被充满水的护城河包围。\ n \ n \ n \ n \ n \ n \ n利希滕斯坦城堡\ n \ n \ n一座城堡(来自拉丁词“castellum”)是中世纪在欧洲和中东建造的一座坚固的建筑。人们争论城堡这个词的含义。但是,它通常意味着贵族或贵族的私人结构。这不同于一个不是住宅的堡垒,也不是一个防御性强的城镇。大约900年前的城堡建成后,他们拥有许多不同的形状和不同的细节。\ n城堡在9世纪和10世纪始于欧洲。他们控制着他们周围的地方,可以帮助攻击和防守。武器可以从城堡发射,或者可以保护城堡中的敌人。但是,城堡也是权力的象征。他们可以用来控制周围的人和道路。\ n许多城堡一开始经常使用体力劳动,用泥土和木头建造,然后用石头代替它们的防御。早期的城堡通常使用自然保护,并没有塔。在晚12和早期的13世纪,虽然,城堡变得更长,更复杂\ n

+0

是什么,输出什么样子的? – ShadowRanger

+0

http://stackoverflow.com/questions/13730107/writelines-writes-lines-without-newline-just-fills-the-file –

回答

1

file.writelines(str(soup.encode('UTF-8')))是怎么样的疯狂,它是:

  1. 编码文本(str)二进制(bytes
  2. str包装获得的该文本表示(所以它需要键入重新二进制字节的东西,但它不是原始的二进制)
  3. 写作,在一个时间导致一个字符(writelines迭代你给它,而str s迭代字符)

步骤3是愚蠢和低效率,但大多是无害的。如果您将原始二进制文件写入以二进制写入打开的文件并且实际编写了bytes对象,那么步骤1将会很顺利。但#1和#2一起意味着像新线那样的东西在输出中被转换为文字\n,而不是实际上打破了一条线。如é这样的非ASCII字符输出为\xc3\xa9,整个事情被包装在b''(或b"")中。

你想要的东西,如:

# open with UTF-8 encoding (in case your system defaults to something else) 
with open("TextOutput.txt", 'w', encoding='utf-8') as file: 
    # Get the text and write it as a single block 
    file.write(soup.get_text()) 
+0

它确实感到疯狂。这产生了我所期望的,谢谢!我想我需要学习更多(我有一些像file.open和file.write开始 - 假设我需要不同的语法/ UTF-8编码)。 – Tom

+0

@ user6217257:我猜你在使用Windows?在Windows上,默认编码通常不是你想要的;它是一个特定于语言环境的ASCII超集(英文和许多西欧语言环境,['cp1252'](https://en.wikipedia.org/wiki/CP1252))。问题是,网页大部分是UTF-8,在这种情况下,你所抓取的页面有一个“↑”字符,它不会出现在CP1252中。如果没有指定可以处理该问题的编码(为了获得最大兼容性和工具友好性,通常需要UTF-8,或者在Windows或UTF-16上),那么在尝试编码为CP1252时会出错。 – ShadowRanger

+0

我在Windows上,这是有趣的信息。这也是为什么通过在Windows中双击运行上述(修改后的脚本)失败给出某种回溯错误?但不是当打印(汤)命令被删除。 – Tom

相关问题