I had set code to crawl headlines from the website https://7news.com.au/news/coronavirus-sa and tried to save headlines into txt file.
I wrote the following code:
import requests
from bs4 import BeautifulSoup as bs
f = open("/Users/j/Desktop/Python/chatbot project/headlines.txt", 'w')
url = f'https://7news.com.au/news/coronavirus-sa'
r = requests.get(url)
soup = bs(r.text, 'html.parser')
headlines = soup.select('h2.Card-Headline')
for h in headlines:
print(h.text)
f.write(h.text)
f.close()
The result of print(h.text)
was:
TENS OF THOUSANDS to spend Christmas in quarantine as Omicron causes COVID carnage
SA records ‘steep increase’ in COVID cases as premier issues ominous warning
South Australia gives nod to widespread rollout of rapid antigen COVID-19 tests
South Australia’s Omicron cases almost TRIPLE as COVID-19 cases surge
Leading doctor’s warning about ‘essential’ and ‘necessary’ spread of COVID-19
SA ambos sound the alarm over rising COVID-19 cases after state records surge
Scott Morrison flags potential changes to COVID-19 approach after National Cabinet
STATE OF THE NATION: Australia fighting to contain COVID as cases soar to record highs
WATCH LIVE: Scott Morrison provides COVID-19 update after National Cabinet meeting
Australia’s COVID cases could hit 250,000 DAILY unless restrictions return
PM’s plea ahead of emergency meeting as he declares ‘we’re not going back to lockdowns’
South Australia scraps testing rule as cases surge to all-time high
The headlines were sorted by lines.
However, when I checked the text file, the result was:
TENS OF THOUSANDS to spend Christmas in quarantine as Omicron causes COVID carnageSA records ‘steep increase’ in COVID cases as premier issues ominous warningSouth Australia gives nod to widespread rollout of rapid antigen COVID-19 tests South Australia’s Omicron cases almost TRIPLE as COVID-19 cases surgeLeading doctor’s warning about ‘essential’ and ‘necessary’ spread of COVID-19SA ambos sound the alarm over rising COVID-19 cases after state records surgeScott Morrison flags potential changes to COVID-19 approach after National CabinetSTATE OF THE NATION: Australia fighting to contain COVID as cases soar to record highsWATCH LIVE: Scott Morrison provides COVID-19 update after National Cabinet meetingAustralia’s COVID cases could hit 250,000 DAILY unless restrictions returnPM’s plea ahead of emergency meeting as he declares ‘we’re not going back to lockdowns’South Australia scraps testing rule as cases surge to all-time high
The line was not separated as expected.
I had tried to split it by recalling the text file and use .split()
method, which did not work.
Is there any way to recall this file and split it with lines, or save it as separated at first?
CodePudding user response:
Try to add \n
in f.write()
so your string h
will write to new line
for h in headlines:
print(h.text)
f.write(h.text "\n")
f.close()