python - 在使用 .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])```
解决方案
str.partition( sep ) 在第一次出现 sep 时拆分字符串,并返回一个 3 元组,其中包含分隔符之前的部分、分隔符本身和分隔符之后的部分。如果未找到分隔符,则返回一个包含字符串本身的 3 元组,后跟两个空字符串。
printy.partition(...)[0]
分隔符之前的内容也是如此,[2]
之后也会有。
推荐阅读
- python - 如何使用 matplotlib 将 3D numpy 数组可视化为 GIF?
- dart - 如果我们在 dart 中使用 const 构造函数创建对象,那么该对象是在编译时创建的吗?
- javascript - 暗黑破坏神 2 - 改进验证符文配方的算法
- python - 使用 Post 方法的 Python requests.request 下载内容不正确的 zip 文件
- tapkey - 在 tapkey 端点上使用 $filter
- javascript - 显示当前用户在不同国家的时间
- python - 将不同大小数组的数组转换为 numpy 数组
- haskell - 将 macOS 更新到 Monterey 后无法为 Haskell 运行 QuickCheck
- java - slide.createTable() 在使用 org.apache.poi 库创建 ppt 时生成异常
- apache - 在 Apache 上进行重定向所需的最小配置是什么?