-3

This is the code that i have written :

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)

The output that is written to the file is :

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

I want the output to be saved like this :

IIT Bombay: Bird’s eye view and quantum biology
April 22, 2017 18:56 IST
  • Just write a newline after `webpage_title`? Are you asking us how to write a newline perhaps? – Martijn Pieters Apr 30 '17 at 10:21
  • @MartijnPieters can we use `f.writelines(webpage_title)` with codecs here? Is there any method like that in codecs. – bhansa Apr 30 '17 at 10:24
  • 2
    @Bhansa: `file.writelines` doesn't write newlines. It writes a *sequence* of text, rather than one element, but newlines are not inserted for you. – Martijn Pieters Apr 30 '17 at 10:25
  • Possible duplicate of [Writing newline with Python codecs.write](http://stackoverflow.com/questions/28439366/writing-newline-with-python-codecs-write) – bhansa Apr 30 '17 at 10:29

2 Answers2

1

Since it is very general, I am just giving an idea for this context.

You need to just put a new line after writing webpage_title.

f.writelines(webpage_title)
f.write("\n")
bhansa
  • 7,282
  • 3
  • 30
  • 55
0

I used windows style "\r\n".It works like a charm :

 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)