簡體   English   中英

如何從python中的Web元素文本中刪除逗號?

[英]How to remove commas from web element text in python?

如何從 WebElement 中刪除,

細節:

我使用 selenium 抓取了一個網頁,但我想從該文本中刪除,

例如:

如果我刮掉hey, welcome 2020然后這將進入我要刪除的 csv 文件,

這是我的代碼:

if (driver.find_elements_by_css_selector('.restaurants-detail-overview-cards-LocationOverviewCard__addressLink--1pLK4 ._2wKz--mA .restaurants-detail-overview-cards-LocationOverviewCard__detailLinkText--co3ei')):
    address = driver.find_elements_by_css_selector('.restaurants-detail-overview-cards-LocationOverviewCard__addressLink--1pLK4 ._2wKz--mA .restaurants-detail-overview-cards-LocationOverviewCard__detailLinkText--co3ei')

else:
    address = 'NONE'

with open(save,'a',encoding='utf-8') as s:
    for i in range(1):
        addresst = address

        if addresst == 'NONE':
            addresst = str(address)
        else:
            addresst = address[i].text

        s.write(addresst + '\n')

replace(',', '')怎么樣? 您也不需要兩次定位元素

address = driver.find_elements_by_css_selector('.restaurants-detail-overview-cards-LocationOverviewCard__addressLink--1pLK4 ._2wKz--mA .restaurants-detail-overview-cards-LocationOverviewCard__detailLinkText--co3ei')
with open(save,'a',encoding='utf-8') as s:
    if address:
        addresst = address[0].texts.replace(',', '')
    else:
        addresst = 'NONE'

    s.write(addresst + '\n')

如果要在 CSV 中寫入 Web 元素文本值而不包含“,”。 您可以使用以下代碼。

if (driver.find_elements_by_css_selector('.restaurants-detail-overview-cards- 
    LocationOverviewCard__addressLink--1pLK4 ._2wKz--mA .restaurants-detail-overview- 
    cards-LocationOverviewCard__detailLinkText--co3ei')):
             address = driver.find_elements_by_css_selector('.restaurants-detail- 
             overview-cards-LocationOverviewCard__addressLink--1pLK4 ._2wKz-- 
             mA.restaurants-detail-overview-cards-              
             LocationOverviewCard__detailLinkText--co3ei')

else:
    address = 'NONE'
with open(save,'a',encoding='utf-8') as s:
    for i in range(1):
        addresst = address

       if addresst == 'NONE':
           addresst = str(address)
       else:
           addresst = address[i].text.replace(',','')

       s.write(addresst + '\n')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM