简体   繁体   English

从字符串中删除不需要的字符

[英]Remove unwanted character from string

from selenium import webdriver
import time
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
import pandas as pd
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import pandas as pd
from csv import writer


options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920x1080")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
wait = WebDriverWait(driver, 10)

url = "https://www.askgamblers.com/online-casinos/reviews/casino-friday"
driver.get(url)


soup = BeautifulSoup(driver.page_source, "html.parser")

product = []

pays=soup.select("div#tabPayments")

for pay in pays:
    try:
        t4=pay.select_one(" .review-details-wrapper+ .review-details-wrapper .review-details__item:nth-child(2) .review-details__text")
        t4 = [i for i in t4 if i.text]
    except:
        pass
    
supports = soup.find("div", {"id": "tabCustomers"})
supports = supports.find("div", {"class": "review-details__text"})
email = "Support Email:"+supports.text.replace("\n", "").split(":")[1]
print(email)

they show me output like that他们像这样给我看 output

['\nSupport\nEmail:\nsupport@casinofriday.com\n', '\n']

but I want output like that:但我想要这样的 output:

 Support Email:support@casinofriday.com 

I want to remove all unwanted character from my string kindly recommend any solution these is the page link https://www.askgamblers.com/online-casinos/reviews/casino-friday我想从我的字符串中删除所有不需要的字符请推荐任何解决方案这些是页面链接https://www.askgamblers.com/online-casinos/reviews/casino-friday

Full Code完整代码

from selenium import webdriver
import time
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
import pandas as pd
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import pandas as pd
from csv import writer


options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920x1080")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
wait = WebDriverWait(driver, 10)

url = "https://www.askgamblers.com/online-casinos/reviews/casino-friday"
driver.get(url)


soup = BeautifulSoup(driver.page_source, "html.parser")

product = []

pays = soup.select("div#tabPayments")

for pay in pays:
    try:
        t4 = pay.select_one(
            " .review-details-wrapper+ .review-details-wrapper .review-details__item:nth-child(2) .review-details__text")
        t4 = [i.replace("\n", "") for i in t4 if i.text]
    except:
        pass
    print(t4)
supports = soup.find("div", {"id": "tabCustomers"})
supports = supports.find("div", {"class": "review-details__text"})
email = "Support Email:"+supports.text.replace("\n", "").split(":")[1]
print(email)

Output Output

['EWallets:0-1 hours', 'Bank Transfers:1-7 days', 'Cheques:Not offered', 'Card Payments:1-7 days', 'Pending Time:0-24 hours']
Support Email:support@casinofriday.com

Hope this helps.希望这可以帮助。 Happy Coding:)快乐编码:)

Looks like you could utilize two methods to achieve your goals: replace() and split() before appending.看起来您可以使用两种方法来实现您的目标:附加之前的 replace() 和 split() 。

You can remove any unwanted characters in the string with use of .replace() method replacing the unwanted character with nothing, empty string.您可以使用.replace()方法删除字符串中不需要的字符,用空字符串替换不需要的字符。
Lets say your result string is stored in product string.假设您的结果字符串存储在product字符串中。 So, to remove the unwanted [ sign you can do this:因此,要删除不需要的[符号,您可以这样做:

product = product.replace('[', '')

Doing that for all the characters you want to remove will lead for code like this:对所有要删除的字符执行此操作将导致如下代码:

product = product.replace('[', '')
product = product.replace('[', '')
product = product.replace('\n', '')
product = product.replace("'", "")
product = product.replace(",", "")
product = product.strip()

The final strip() removes leading and trailing spaces.最后的strip()删除前导和尾随空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM