I have a question about deleting spaces

Asked 1 years ago, Updated 1 years ago, 444 views

\t\n\r when you import and print the code to get the school notice and convert it to a csv file

0,"


                                [Announcement] Information on the Humanities Convergence Project Briefing Session



                            "


1,"


                                44th Air Force Chief of Staff Space Challenge 2023 Gyeongin Regional Preliminary Competition 



                            "
2,"

There are many spaces together, so even if you remove the space using replace, the space does not disappear Please reply.

//import requests
from bs4 import BeautifulSoup
import pandas as pd

url = 'https://seowon-h.goeyi.kr/seowon-h/na/ntt/selectNttList.do?mi=16474&bbsId=9508'


response = requests.get(url)


html = response.text
soup = BeautifulSoup(html, 'html.parser')
now = soup.find_all(class_='nttInfoBtn')

ha = []

for i in now:
    ha.append(i.text)

# Up to the top, ha is on the list
str1 = ""
str1 = "".join(ha)
str1 = str1.replace('','') #blankdelete
#So far, ha is absorbed by str1

fin =list(str1)
print(fin)


df = pd.DataFrame(fin)


df.to _csv ('School Notice.csv')

beautifulsoup replace python

2023-03-26 08:43

1 Answers

To clear the spaces before and after, write strip() instead of replace().

And the point at which the gaps are removed is wrong. Try it like this:

from bs4 import BeautifulSoup
import requests

url = 'https://seowon-h.goeyi.kr/seowon-h/na/ntt/selectNttList.do?mi=16474&bbsId=9508'

response = requests.get(url)

html = response.text
soup = BeautifulSoup(html, 'html.parser')
now = soup.find_all(class_='nttInfoBtn')

ha = []

for i in now:
    ha.append(i.text.strip() + '\n')

str1 = ""
str1 = "".join(ha)

print(str1)


2023-03-26 10:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.