将 Beautifulsoup4 中的网络抓取数据保存为 txt 文件后更改行
Change line after saving web crawled data from Beautifulsoup4 as txt file
我已设置代码以从网站 https://7news.com.au/news/coronavirus-sa 抓取标题,并尝试将标题保存到 txt 文件中。
我写了下面的代码:
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()
print(h.text)
的结果是:
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
标题按行排序。
然而,当我检查文本文件时,结果是:
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
行没有按预期分开。
我曾尝试通过调用文本文件并使用 .split()
方法来拆分它,但没有成功。
有没有什么办法可以调出这个文件并用行分割,或者先分开保存?
尝试在 f.write()
中添加 \n
这样您的字符串 h
将写入新行
for h in headlines:
print(h.text)
f.write(h.text+"\n")
f.close()
我已设置代码以从网站 https://7news.com.au/news/coronavirus-sa 抓取标题,并尝试将标题保存到 txt 文件中。
我写了下面的代码:
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()
print(h.text)
的结果是:
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
标题按行排序。
然而,当我检查文本文件时,结果是:
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
行没有按预期分开。
我曾尝试通过调用文本文件并使用 .split()
方法来拆分它,但没有成功。
有没有什么办法可以调出这个文件并用行分割,或者先分开保存?
尝试在 f.write()
中添加 \n
这样您的字符串 h
将写入新行
for h in headlines:
print(h.text)
f.write(h.text+"\n")
f.close()