Issue
I am trying to write the data to a text file that I scraped using BeautifulSoup It prints the data in the console but not to the file
import requests
from bs4 import BeautifulSoup
base_url = 'https://www.aaabbbccc.com'
r = requests.get(base_url)
soup = BeautifulSoup(r.text)
outF = open("myOutFile.txt", "w", encoding='utf-8')
for story_heading in soup.find_all(class_="col-md-4"):
if story_heading.a:
print(story_heading.a.text.replace("\n", " ").strip())
outF.write(str(story_heading))
outF.write("\n")
else:
print(story_heading.contents[0].strip())
outF.close()
Solution
I would always use a+
method!
If the text file doesn't exist on your hard-drive, it'll create it and write to it. If the text file exists, it'll append your content to the end of it!
with open("myOutFile.txt", "a+") as f:
import requests
from bs4 import BeautifulSoup
base_url = 'https://www.aaabbbccc.com'
r = requests.get(base_url)
soup = BeautifulSoup(r.text)
with open("myOutFile.txt", "a+", encoding='utf-8') as f:
for story_heading in soup.find_all(class_="col-md-4"):
if story_heading.a:
print(story_heading.a.get_text())
f.write(str(story_heading)+"\n")
else:
print(story_heading.contents[0].strip())
Answered By - Ruvee
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.