首页 > 解决方案 > 在使用 .partition 的 selenium 中,我的代码只打印子字符串之前的内容,而不是子字符串之后的内容

问题描述

在使用 .partition 的 selenium 中,我的代码只打印子字符串之前的内容,而不是子字符串之后的内容。

我得到的响应是 stylecolor 之前的所有内容,应该是之后的所有内容

import time

options = webdriver.ChromeOptions()


driver = webdriver.Chrome(options=options, executable_path=r"/Users/husaynjaffer/Desktop/chromedriver")
url = "https://api.nike.com/product_feed/threads/v2/?filter=marketplace%28CA%29&filter=language%28en-GB%29&filter=channelId%28010794e5-35fe-4e32-aaff-cd2c74f89d61%29&filter=exclusiveAccess%28true,false%29"
driver.get(url)
printy= (driver.find_element_by_xpath("/html/body").text)





print (printy.partition(",\"styleColor\":\"")[0])```

标签: pythonselenium

解决方案


str.partition( sep ) 在第一次出现 sep 时拆分字符串,并返回一个 3 元组,其中包含分隔符之前的部分、分隔符本身和分隔符之后的部分。如果未找到分隔符,则返回一个包含字符串本身的 3 元组,后跟两个空字符串。

printy.partition(...)[0]分隔符之前的内容也是如此,[2]之后也会有。


推荐阅读