Home > Blockchain >  How to replace special characters in string - selenium python
How to replace special characters in string - selenium python

Time:10-08

I have a piece of code as follows. I want to take a header and remove the special symbols " !@#$%^&* " from it, but I've tried everything but still can't. Hope everyone can help, thank you very much

        try:
        title = driver.find_element(By.XPATH,'/html/body/main/section[2]/div/div/article/div[3]/p[1]/span').text
        print(title)

        if title.count("#") > 0:
            titles.append(title)
            titles[number] = title[0:title.index('#')]
            number  = 1
        else:
            titles.append(title)
            number  = 1

        if titles[number-1] == '':
            titles[number-1] = f"Invalid Title"
            
        banned_char = '<>:"/\|?*'
        for character in banned_char:
            if title.count(character) > 0:
                titles[number-1] = title[title.replace('<>:"/\|?*',' ')]
    except:
        titles.append(f'Failed Title number {number}')
        number =1
        print(f'Download {number} have no title.')

CodePudding user response:

I see two mistakes in your code

  • replace searchs exactly string '<>:"/\|?*' and you should replace every char separatelly .replace('<',' ').replace('>',' ').replace(':',' ') (or run it in for-loop)

  • you have to assign title = title.replace(), not title[title.replace(...)

banned_char = '<>:"/\|?*'

for character in banned_char:
    title = title.replace(character,' ')

# --- after loop ---

titles[number-1] = title
  • Related