[英]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.