0

I want to extract news headlines from Hindustantimes.com. I have the following code but i get the data in different columns in the same row. I want each news on separate line. Can anyone please help me. Thanks in advance. Here is my code:

import requests
import bs4
import csv
res=requests.get('https://www.hindustantimes.com')
soup=bs4.BeautifulSoup(res.text,'lxml')

x=[]
for i in soup.select('div.subhead4'):
    x.append(i.text)

for i in soup.select('div.bigstory-mid-h3'):
    x.append(i.text)

for i in x:
    print(i)

with open('newz.csv','w') as cF:
    wr = csv.writer(cF)
    wr.writerow(x)

3 Answers 3

4

writerow takes an iterable to write:

writerow(['a', 'b', 'c'])
-> a,b,c

Also, you don't need to append the data to a list before you write.

stories = soup.select('div.subhead4') + soup.select('div.bigstory-mid-h3')

with open('output.csv', 'w') as f:
    writer = csv.writer(f)
    for headline in stories:
        writer.writerow([headline.text.strip()])
Sign up to request clarification or add additional context in comments.

1 Comment

This makes more sense than doing the other code. Nice work!
1
res = requests.get('https://www.hindustantimes.com')
soup = bs4.BeautifulSoup(res.text,'lxml')

x=[]
for i in soup.select('div.subhead4'):
    x.append(i.text.strip())

for i in soup.select('div.bigstory-mid-h3'):
    x.append(i.text.strip())


with open('newz.csv','w') as cF:
    wr = csv.writer(cF)
    for i in x:
        wr.writerow([i])

Comments

0

it simple .csv file, here without using csv module

with open('newz.csv','w') as cF:
  for x in soup.select('.subhead4 a'):
    cF.write(x.text + '\n')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.