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

2hh7jdfx  于 2022-12-13  发布在  其他
关注(0)|答案(3)|浏览(172)
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)

他们给我看了这样输出

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

但我希望输出是这样的:

Support Email:support@casinofriday.com

我想从我的字符串中删除所有不需要的字符,请推荐任何解决方案,这些是页面链接https://www.askgamblers.com/online-casinos/reviews/casino-friday

icnyk63a

icnyk63a1#

完整代码

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)

输出

['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

希望这对你有帮助。快乐编码:)

esbemjvw

esbemjvw2#

看起来你可以利用两种方法来实现你的目标:replace()和split(),然后再追加。

mpgws1up

mpgws1up3#

您可以使用.replace()方法删除字符串中任何不需要的字符,将不需要的字符替换为空字符串。
假设结果字符串存储在product string中,那么,要删除不需要的[符号,可以这样做:

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

对所有要删除的字符执行此操作将导致以下代码:

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

最后的strip()会移除开头和结尾的空格。

相关问题