Py学习  »  Python

如何在python中从web元素中移除coma?

Sumaria Iqbal • 6 年前 • 2257 次点击  

如何删除 , 来自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')
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51379
文章 [ 2 ]  |  最新文章 6 年前
pasupati
Reply   •   1 楼
pasupati    6 年前

如果要在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')
Guy
Reply   •   2 楼
Guy    6 年前

怎么样 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(',', '') + '\n'
    else:
        addresst = 'NONE'

    s.write(addresst + '\n')