【问题标题】:Why cant i append data to a newline in python 2.7?为什么我不能在 python 2.7 中将数据附加到换行符?
【发布时间】:2017-09-28 01:21:38
【问题描述】:

这是我写的代码:

import urllib2
import codecs
import urllib
import re
from bs4 import BeautifulSoup 
from lxml.html import fromstring
import codecs

url="http://www.thehindu.com/sci-tech/science/iit-bombay-birds-eye-view-and-quantum-biology/article18191268.ece"
htmltext = urllib.urlopen(url).read()
resp = urllib.urlopen(url)
respData =resp.read()
paras = re.findall(r'<p>(.*?)</p>',str(respData))
soup = BeautifulSoup(htmltext,"lxml")

webpage_title = soup.find_all('h1', attrs = {"class": "title"})
webpage_title = webpage_title[0].get_text(strip=True)
with codecs.open("E:\\Crawler_paras_sorted_test_webpages_complete.txt", "w+", encoding="utf-8") as f:
    f.write(webpage_title)

soup = BeautifulSoup(htmltext,"lxml")
ut_container = soup.find("div", {"class": "ut-container"})
time = ut_container.find("none").text.strip()
with codecs.open("E:\\Crawler_paras_sorted_test_webpages_complete.txt", "a+",encoding="utf-8") as f:
    f.write(time)

写入文件的输出是:

IIT Bombay: Bird’s eye view and quantum biologyApril 22, 2017 18:56 IST

我希望这样保存输出:

IIT Bombay: Bird’s eye view and quantum biology
April 22, 2017 18:56 IST

【问题讨论】:

  • 只是在webpage_title之后写一个换行符?您是在问我们如何编写换行符吗?
  • @MartijnPieters 我们可以在这里使用f.writelines(webpage_title) 和编解码器吗?编解码器中是否有类似的方法。
  • @Bhansa:file.writelines 不写换行符。它写入一个 sequence 文本,而不是一个元素,但不会为您插入换行符。

标签: python-2.7 newline


【解决方案1】:

我使用了 windows 样式“\r\n”。它的工作原理就像一个魅力:

 import urllib2
import codecs
import urllib
import re
from bs4 import BeautifulSoup 
from lxml.html import fromstring
import codecs

url="http://www.thehindu.com/sci-tech/science/iit-bombay-birds-eye-view-and-quantum-biology/article18191268.ece"
htmltext = urllib.urlopen(url).read()
resp = urllib.urlopen(url)
respData =resp.read()
paras = re.findall(r'<p>(.*?)</p>',str(respData))
soup = BeautifulSoup(htmltext,"lxml")

webpage_title = soup.find_all('h1', attrs = {"class": "title"})
webpage_title = webpage_title[0].get_text(strip=True)
with codecs.open("E:\\Crawler_paras_sorted_test_webpages_complete.txt", "w+", encoding="utf-8") as f:
    f.write(webpage_title+"\r\n")

soup = BeautifulSoup(htmltext,"lxml")
ut_container = soup.find("div", {"class": "ut-container"})
time = ut_container.find("none").text.strip()
with codecs.open("E:\\Crawler_paras_sorted_test_webpages_complete.txt", "a+",encoding="utf-8") as f:
    f.write(time)

【讨论】:

    【解决方案2】:

    由于它非常笼统,我只是针对这种情况给出一个想法。

    你只需要在写webpage_title之后换一个新行。

    f.writelines(webpage_title)
    f.write("\n")
    

    【讨论】:

    • 换行符没有附加到文件中。数据被写入到它旁边。
    猜你喜欢
    • 2014-10-10
    • 2014-01-30
    • 2014-03-28
    • 1970-01-01
    • 2017-12-23
    • 2021-03-26
    • 2017-11-17
    • 2021-11-07
    • 1970-01-01
    相关资源
    最近更新 更多